4

I have a method and I want to add this method as an extension method to properties of my class. This method give an expression as input parameter. The method is like below :

    public static string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
    {
        return (propertyExpression.Body as MemberExpression).Member.Name;
    }

I want to use this method like below example :

string propertyName = MyClass.Property1.GetPropertyName();

Is it possible? if yes, what is the solution?

masoud ramezani
  • 22,228
  • 29
  • 98
  • 151
  • Possible duplicate of [Linq expressions and extension methods to get property name](http://stackoverflow.com/questions/5252176/linq-expressions-and-extension-methods-to-get-property-name) – Michael Freidgeim Apr 23 '16 at 13:26

4 Answers4

6

No, it's not possible to do that. It's not clear whether MyClass is the name of a class (and Property1 is a static property) or whether it's an instance property and MyClass.Property1 simply isn't a valid member access. If it's the latter, you probably want to change your method to something like:

public static string GetPropertyName<TSource, TResult>(
    Expression<Func<TSource, TResult>> propertyExpression)
{
    return (propertyExpression.Body as MemberExpression).Member.Name;
}

and call it as:

string propertyName = GetPropertyName<MyClass, string>(x => x.Property1);

Or you could use a generic class with a generic method, so that string can be inferred:

string propertyName = PropertyUtil<MyClass>.GetPropertyName(x => x.Property1);

That would be something like:

public static class PropertyUtil<TSource>
{
    public static string GetPropertyName<TResult>(
        Expression<Func<TSource, TResult>> propertyExpression)
    {
       return (propertyExpression.Body as MemberExpression).Member.Name;
    }
}
Ben
  • 7
  • 3
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Extension methods are type-specific (even when they are generic), you can't have an extension method for a property without having the same extension method available to all items that are of that type.

If you have an extension method for a specific type, it won't matter if it's a Property or a local variable or a class member, the extension method will still be availabe.

jmoreno
  • 12,752
  • 4
  • 60
  • 91
0

You can't create "static" extension methods.

Extension methods are "instance" methods.

You could just provide a helper class:-

string propertyName = PropertyHelper.GetPropertyName(() => instanceOfMyClass.Property1);
BenCr
  • 5,991
  • 5
  • 44
  • 68
0

I'm clearly not an expert like Jon but this seems to me that what you want and is fairly simple (that's why I doubt, since Jon is clearly a reference person ! ;-)) :

class Program
{
    static void Main(string[] args)
    {
        MyClass<int> myClass = new MyClass<int>();
        string property1Name = myClass.Property1.GetPropertyName();
        string property2Name = myClass.Property2.GetPropertyName();
    }
}

public static class Extensions
{
    public static string GetPropertyName<T>(this Expression<Func<T>> propertyExpression)
    {
        return (propertyExpression.Body as MemberExpression).Member.Name;
    }
}

public class MyClass<T>
{
    public Expression<Func<T>> Property1; //Sample with MyClass being generic
    public Expression<Func<string>> Property2; //Sample which works anyway, MyClass being generic or not
}

It compiles and meet the requirements, from what I can see. You probably should have found it yourself, since you spoke about an extension method from start...

Ssithra
  • 710
  • 3
  • 8
  • My class is a simple class with simple properties that have simple type like int or bool. In your class this properties isn't simple; – masoud ramezani May 28 '11 at 10:38
  • In your original post, the **propertyExpression** input parameter of **GetPropertyName** was of type `Expression>`. That why I reproduced such types for the properties in my sample. If that's not what you want, I must have missed something. I think I finally don't undestand your question... – Ssithra May 28 '11 at 17:54