4.5 中的 CallerMemberName 属性“能够被伪造"吗? [英] Is the CallerMemberName attribute in 4.5 "able to be faked"?

查看:19
本文介绍了4.5 中的 CallerMemberName 属性“能够被伪造"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此 .NET 4.5 引入了 CallerMemberNameAttribute,这对于使用 WPF 并实现 INotifyPropertyChanged 的任何人来说似乎是天赐之物 - 我的问题是:属性是否具有内在联系/4.5 5.0 编译器支持,或者它更像是环境中的语法糖助手,就像可以通过声明自己的 ExtensionAttribute 来伪造 Visual Studio,神奇地开启 LINQ 语法?

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.5 5.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 ExtensionAttribute of your own, magically turning on LINQ syntax?

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

(sorry Jon!) I guess I'm asking if one can "enable" the functionality of the CallerMemberNameAttribute in .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 ExtensionAttribute class. My strong suspicion is no, naturally...

换一种方式:我想知道我是否可以在不升级到 .NET 4.5/5 的情况下从 CallerMemberName 的功能中受益

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

希望更清楚...

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

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

推荐答案

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

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:

namespace System.Runtime.CompilerServices {

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

}

非常注意项目中的每个人都在使用 VS2012,否则他们会因为 CallerMemberNameAttribute 没有做任何事情而 null 作为参数默认传递.

Be very careful that everyone on the project is using VS2012, otherwise they'll get silent bugs because CallerMemberNameAttribute didn't do anything and null was passed as the parameter default.

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

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

这篇关于4.5 中的 CallerMemberName 属性“能够被伪造"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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