wpf 4.5 中的 CallerMemberName 属性是否“能够被伪造”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13381917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:12:43  来源:igfitidea点击:

Is the CallerMemberName attribute in 4.5 "able to be faked"?

.netwpf.net-4.5

提问by JerKimball

So .NET 4.5 introduces the CallerMemberNameAttribute, which seems like a godsend to anyone working with WPF and implementing INotifyPropertyChanged- my question is this: Is the attribute intrinsically tied/supported by the 4.55.0 compiler, or is it more of a syntactical sugar helper by the environment, much like one could fake out Visual Studio by declaring an ExtensionAttributeof your own, magically turning on LINQ syntax?

所以 .NET 4.5 引入了CallerMemberNameAttribute,这对于任何使用 WPF 和实现的人来说似乎是天赐之物INotifyPropertyChanged- 我的问题是:该属性是否由4.55.0 编译器固有地绑定/支持,或者它更像是环境的语法糖助手,就像您可以通过声明ExtensionAttribute自己的,神奇地打开 LINQ 语法来伪造 Visual Studio 一样?

Edit: (sorry Jon!) I guess I'm asking if one can "enable" the functionality of the CallerMemberNameAttributein .NET 4.0 via redeclaration of the attribute in the proper namespace, much like one can "enable" LINQ query syntax in .NET 2.0 by proper declaration of the ExtensionAttributeclass. My strong suspicion is no, naturally...

编辑:(对不起 Jon!)我想我是在问是否可以CallerMemberNameAttribute通过在适当的命名空间中重新声明属性来“启用” .NET 4.0 中的功能,就像可以在 .NET 中“启用”LINQ 查询语法一样2.0 通过正确声明ExtensionAttribute类。我强烈的怀疑是没有,自然...

Put yet another way: I want to know if I can benefit from the functionality of CallerMemberNamewithout upgrading to .NET 4.5/5

换句话说:我想知道我是否可以CallerMemberName从不升级到 .NET 4.5/5的功能中受益

Hopefully that's more clear...

希望这更清楚......

NinjaEdit #2: Sigh...version numbering conventions are confusing!

NinjaEdit #2:叹息……版本编号约定令人困惑!

回答by Julien Lebosquain

Yes, you can, exactly as you could use LINQ and .NET 2, as you said. I use the following in a .NET 4.0 project with the VS2012 compiler with success:

是的,正如您所说,您可以使用 LINQ 和 .NET 2。我在带有 VS2012 编译器的 .NET 4.0 项目中成功使用了以下内容:

namespace System.Runtime.CompilerServices {

    [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
    public sealed class CallerMemberNameAttribute : Attribute {
    }

}

Be verycareful that everyone on the project is using VS2012, otherwise they'll get silent bugs because CallerMemberNameAttributedidn't do anything and nullwas passed as the parameter default.

非常小心,大家对项目使用VS2012,否则他们会得到错误沉默,因为CallerMemberNameAttribute没有做任何事情,null作为参数默认传递。

Edit 2013-06-28:Consider installing the Microsoft.BclNuGet package that provides CallerMemberNameAttribute(and some other classes from .NET 4.5) for .NET 4 and Silverlight rather than doing it manually.

编辑 2013-06-28:考虑安装为 .NET 4 和 Silverlight提供(以及 .NET 4.5 中的其他一些类)的Microsoft.BclNuGet 包,CallerMemberNameAttribute而不是手动安装。