How can I add this method as an extension method to properties of my class?
Asked Answered
V

4

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?

Veiling answered 18/5, 2011 at 10:3 Comment(1)
Possible duplicate of Linq expressions and extension methods to get property nameHoraciohorae
U
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;
    }
}
Urn answered 18/5, 2011 at 10:11 Comment(0)
D
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.

Dyslogistic answered 27/5, 2011 at 18:26 Comment(0)
H
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);
Haroldson answered 18/5, 2011 at 10:11 Comment(0)
F
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...

Fairway answered 27/5, 2011 at 18:5 Comment(2)
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;Veiling
In your original post, the propertyExpression input parameter of GetPropertyName was of type Expression<Func<T>>. 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...Fairway

© 2022 - 2024 — McMap. All rights reserved.