是否可以重写这个棘手的扩展方法?
是否可以重写这个不带参数的扩展方法?
public static string PropertyName<T>(this T obj, Expression<Func<T>> property)
{
var memberExpression = property.Body as MemberExpression;
if (memberExpression == null)
throw new ArgumentException("Expression must be a MemberExpression.", "property");
return memberExpression.Member.Name;
}
这是该函数的测试代码。
string TEST = Guid.NewGuid().ToString();
string propertyName = TEST.PropertyName(() => TEST);
这就是结果:
propertyName = "TEST"
我想将代码重写为:
string propertyName = TEST.PropertyName();
Ps。我对此方法的静态版本不感兴趣!
对于那些不明白这个功能的意义的人来说。 在 MVVM 模式中,您已通知属性发生更改。 像这样。
this.RaisePropertyChanged("TEST");
这是一个不好的方法,因为属性名称是硬编码的。 在扩展方法的帮助下,您将得到:
this.RaisePropertyChanged(()=>Test);
我想将扩展方法重写为:
this.RaisePropertyChanged(Test.PropertyName());
下面是我的 MVVM 项目的代码示例。 (这是模型属性)
public DateTime Start
{
get { return WorkModel.Start; }
set
{
if (WorkModel.Start != value)
{
WorkModel.Start = new DateTime(SelectedDate.Year, SelectedDate.Month, SelectedDate.Day, value.Hour, value.Minute, value.Second);
this.RaisePropertyChanged("Start");
this.RaisePropertyChanged("TotalWork");
}
}
}
Is it possible to rewrite this extension method without the parameter?
public static string PropertyName<T>(this T obj, Expression<Func<T>> property)
{
var memberExpression = property.Body as MemberExpression;
if (memberExpression == null)
throw new ArgumentException("Expression must be a MemberExpression.", "property");
return memberExpression.Member.Name;
}
This is the test code for the function.
string TEST = Guid.NewGuid().ToString();
string propertyName = TEST.PropertyName(() => TEST);
This is the result:
propertyName = "TEST"
I would like to rewrite the code to this:
string propertyName = TEST.PropertyName();
Ps. I'm not interested in a static version of this method!
For those how doesn’t see the point of this function.
In MVVM pattern you have notify changed for a property.
Like this.
this.RaisePropertyChanged("TEST");
This is a bad approach since the property name is hardcoded.
With help of the extension method you will have this:
this.RaisePropertyChanged(()=>Test);
I would like to rewrite the extension method to this:
this.RaisePropertyChanged(Test.PropertyName());
Below is a code sample from my MVVM project. (this is a Model property)
public DateTime Start
{
get { return WorkModel.Start; }
set
{
if (WorkModel.Start != value)
{
WorkModel.Start = new DateTime(SelectedDate.Year, SelectedDate.Month, SelectedDate.Day, value.Hour, value.Minute, value.Second);
this.RaisePropertyChanged("Start");
this.RaisePropertyChanged("TotalWork");
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,这是不可能的。局部变量名仅供编译器使用,在编译过程中不会被记录或重命名。
当然,属性是不同的东西。您应该知道您的第一个示例与第二个示例不同。
尽管如此,我还是看不出有什么方法可以实现这一点。即使那样,它也会比您的工作 ()=>PropertyName 类型的调用更大的性能杀手。
No this is not possible. Local variable name is only for compiler and is not recorded or can be renamed during compilation.
Of course properties are different thing. And you should be aware your first example is different from your second one.
Still, I dont see a way you can implement this. And even then it would be much bigger performance-killer than your working ()=>PropertyName kind of invocation.