-2

This is my generic Container class:

public class Container<T> : IContainer<T> where T : BaseModel, new()
{
    private string include;

    public Container()
    {
    }

    public Container<T> Include(Expression<Func<T>> property)
    {            
        include = GetMemberName(property);
        return this;
    }
}

Now I want to set the include value like this:

var container = new Container<TestClass>();
// doesn't work
container.Include(x => x.SomeProperty);
// also doesn't work
container.Include(() => TestClass.SomeProperty);

And as result the include should habe the value SomeValue. I also tried a parameterless Function, in the latter case VS says it's missing an object reference for the non-static property.

I got the GetMemberName from this thread: [Retrieving Property name from lambda expression

Daniel
  • 511
  • 10
  • 25

1 Answers1

1

Change your func definition:

public Container<T> Include(Expression<Func<T, object>> property)
{            
    include = GetMemberName(property);
    return this;
}

This is the correct usage:

container.Include(x => x.SomeProperty);
Steve Harris
  • 5,014
  • 1
  • 10
  • 25