cbordeman
asked on
Expression<Func<T>> in parameter list always seen as Func<T>, can't pass p=>p.Name!!
I'm trying to create a method that takes a field name using the cool type safe format popular these days, but my calling code won't compile.
Here is the method:
Now the calling code:
The problem is
A) It thinks I am passing a Func<T> to MyMethod(), somehow it isn't recognizing this is an Expression<Func<T>>!!
B) (probably because of A) the type of p isn't recognized and so it's members are not known. If I let Visual Studio generate a stub, it generates this:
Here is the method:
public void MyMethod<T>(params Expression<Func<T>> propertyExpression)
{
// extract property name from propertyExpression using
// whatever means, then use it to do some other stuff...
}
Now the calling code:
public string Name { get; set; };
obj.MyMethod(p => p.Name);
The problem is
A) It thinks I am passing a Func<T> to MyMethod(), somehow it isn't recognizing this is an Expression<Func<T>>!!
B) (probably because of A) the type of p isn't recognized and so it's members are not known. If I let Visual Studio generate a stub, it generates this:
public void MyMethod<T>(Func<T> func)
{
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.