Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
718 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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