我有一个面向 Windows Phone 7.1+ 和 Windows 应用商店应用程序(适用于 WinRT)的可移植库,它使用 .net 4.5 框架。
我想在其中使用新的 [CallerMemberName]
属性。但是,VS2012 告诉我这个属性在我的可移植库中不可用(这似乎很正常,因为它在 WP7.1 项目中不可用)。
然而,我发现我可以创建自己的属性,编译器会像理解真实属性一样理解它,方法是使用以下代码片段:
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
public sealed class CallerMemberNameAttribute : Attribute { }
}
但是,一旦尝试对其进行编译,我就会收到错误消息 The type 'System.Runtime.CompilerServices.CallerMemberNameAttribute' exists in both 'Portable.dll' and 'mscorlib.dll'
。
我理解消息,但我想知道是否可以在我的可移植库中使用 [CallerMemberName]
?我可能错过了什么。
最佳答案
使用 BCL Portability Pack它为旧版本提供了这些属性。
This packages enables projects targeting .NET Framework 4, Silverlight 4 and 5, and Windows Phone 7.5 (including any portable library combinations) to use new types from later versions of .NET including:
CallerMemberNameAttribute
CallerLineNumberAttribute
CallerFilePathAttribute
...
关于c# - 在可移植库中使用 CallerMemberName 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16564879/