ReflectedType from MemberExpression

Given this code:

static void Main(string[] args)
{
    Expression<Func<SomeDerivedClass, object>> test = i => i.Prop;
    var body = (UnaryExpression) test.Body;
    Console.WriteLine(((MemberExpression) body.Operand).Member.ReflectedType);
}

public class SomeClass
{
    public int Prop { get; private set; }
}

public class SomeDerivedClass : SomeClass
{
}

I would expect ReflectedType to be SomeDerivedClass, given that it is the type of parameter for the expression. But this is SomeClass, which - if I understand correctly - is the type of declaration.

Why is this?

+4
source share
3 answers

. , , , #, . # DeclaringType, IL. , ReflectedType . ReflectedType , PropertyInfo .

, , :

public class SomeClass
{
    public int Prop { get; set; }
}

public class SomeDerivedClass : SomeClass
{
    public int get_Prop() { return 4; }
}

Prop getter , getter, get_Prop, . , Prop SomeClass.get_Prop. SomeDerivedClass.get_Prop , Prop , SomeDerivedClass.

, , # PropertyInfo, getter , PropertyInfo, : getter SomeClass, PropertyInfo SomeClass.

+4

, Prop, SomeClass, SomeDerivedClass. , SomeDerivedClass lambda- test, , UnaryExpression , SomeClass.

Prop SomeDerivedClass, .

+1

ReflectedType Type, MemberInfo. DeclaringType, MemberInfo , .

http://msdn.microsoft.com/en-us/library/system.reflection.memberinfo.reflectedtype.aspx

-1

Source: https://habr.com/ru/post/1536749/


All Articles