Solved

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

Posted on 2011-02-24
1
713 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

688 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