0

For example using this classes:

Public class InnerClass
{
    string Value {get;set;}
}

public class ObjectClass
{
    InnerClass Inner {get; set;}
}

And with a method like this:

public void SomeMethod<TObj, T>(Expression<Func<TObj, T>> exp) where T : InnerClass
{
    // ??
}

I need to add "Value" property to the exp.

Calling the method:

SomeMethod(x => x.Inner);

I need to add value to the expression:

x => x.Inner.Value
Luis
  • 436
  • 4
  • 11

3 Answers3

0

Tell it what types you're using, otherwise it won't know:

SomeMethod<ObjectClass, InnerClass>(k => k.Inner);
McAden
  • 13,714
  • 5
  • 37
  • 63
0

Update your method to

public void SomeMethod<TObject>(Expression<Func<TObject, object>> exp)
{
     .....
}

then you can access the property

SomeMethod<ObjectClass>(x => x.Inner.Value);
Sunil Shrestha
  • 303
  • 1
  • 7
0

At the end this work for me:

public void SomeMethod<TObj, T>(Expression<Func<TObj, T>> exp) where T : InnerClass
{
    var newExp = Expression.Lambda<Func<TObj, string>>(
        Expression.PropertyOrField(exp.Body, "Value"),
        exp.Parameters);

    // now I can use newExp
}

From here: Adding a node/property to an Expression Tree

Luis
  • 436
  • 4
  • 11