Link to home
Start Free TrialLog in
Avatar of cbordeman
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:
 
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...
}

Open in new window


Now the calling code:

 
public string Name { get; set; };
obj.MyMethod(p => p.Name);

Open in new window


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)
{
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of cbordeman
cbordeman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial