字符串化成员名称,类似于C中的#define str(s)#s [英] Stringify member name similar to #define str(s) #s in C

查看:282
本文介绍了字符串化成员名称,类似于C中的#define str(s)#s的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以像在C中使用#define str(s) #s宏那样在.NET 4.0的C#中对成员名称进行字符串化?

Is there any way to stringify a member name in C# for .NET 4.0 like you would in C by using the #define str(s) #s macro?

public Double MyDouble
{
    get { return _MyDouble;}
    set 
    { 
        _MyDouble = value; 
        RaisePropertyChanged("MyDouble");
        // The below item would refactor correctly if C# had a stringify pre-processor
        // RaisePropertyChanged(str(MyDouble));
    }
}
private Double _MyDouble;

如果禁用了Search in Strings,则重新分解会中断raise属性更改事件,如果启用,则会破坏完全不相关的字符串.有时,直到UI元素不再响应更改时,我才注意到.

Re-factorization breaks the raise property changed event if I have Search in Strings disabled or breaks completely unrelated strings if it is enabled. Sometimes I won't notice until a UI element no longer responds to changes.

推荐答案

不幸的是,没有.当前,您必须使用PostSharp或 NotifyPropertyWeaver 之类的东西来可靠地做到这一点.

No, unfortunately. Currently you would have to use something like PostSharp or NotifyPropertyWeaver to do this reliably.

请注意,在C#5中,您可以执行以下操作:

Note that in C# 5 you'll be able to do this:

public void RaisePropertyChanged([CallerMemberName] string member = "")
{
}

您将像这样使用它:

public Double MyDouble
{
    get { return _MyDouble;}
    set 
    { 
        _MyDouble = value; 
        RaisePropertyChanged();
    }
}

C#5编译器将自动填写可选参数.

And the C# 5 compiler will automatically fill in the the optional parameter.

这篇关于字符串化成员名称,类似于C中的#define str(s)#s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆