?
Solved

Expression<Func<T>> in parameter list always seen as Func<T>, can't pass p=>p.Name!!

Posted on 2011-02-24
1
Medium Priority
?
721 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:cbordeman
1 Comment
 

Accepted Solution

by:
cbordeman earned 0 total points
ID: 34974652
Dang it.  I just realized I was writing the Func incorrectly.  It should be () => Name not p => p.Name.  I was confused by the Func<T> in the expression.

Tricky tricky.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question