开发者

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

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, wha开发者_开发技巧t is the solution?


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


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜