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

廉价感情. 提交于 2019-12-01 04:27:57

问题


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?


回答1:


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;
    }
}



回答2:


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.




回答3:


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);



回答4:


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...



来源:https://stackoverflow.com/questions/6042937/how-can-i-add-this-method-as-an-extension-method-to-properties-of-my-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!