[英]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, what 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. 目前尚不清楚
MyClass
是否是类的名称(而Property1
是静态属性)或者它是否是实例属性而MyClass.Property1
不是有效的成员访问权限。 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
:
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...
您可能应该自己找到它,因为您从一开始就谈到了扩展方法...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.