我有一个可移植的库,目标是Windows Phone 7.1+和Windows Store应用程序(用于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]?我可能漏掉了什么。
发布于 2013-05-15 20:22:52
使用为旧版本提供这些属性的BCL Portability Pack。
此程序包使面向.NET Framework4、Silverlight4和5以及Windows Phone 7.5 (包括任何可移植的库组合)的项目能够使用更高版本的.NET中的新类型,包括:
https://stackoverflow.com/questions/16564879
复制相似问题