Solved

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

Posted on 2011-02-24
1
705 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now