Using CallerMemberName attribute in a portable library

扶醉桌前 提交于 2019-12-10 06:47:59

问题


I have a portable library that targets Windows Phone 7.1+ and Windows Store apps (for WinRT), which uses the .net 4.5 framework.

I would like to use the new [CallerMemberName] attribute in it. However, VS2012 told me that this attribute is not available in my portable library (that seems normal because it's not available in a WP7.1 project).

Yet, I have found out that I can create my own attribute and the compiler will understand it like the real one, by using this snippet:

namespace System.Runtime.CompilerServices
{
  [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
  public sealed class CallerMemberNameAttribute : Attribute { }
}

However, as soon as try to compile it, I get the error The type 'System.Runtime.CompilerServices.CallerMemberNameAttribute' exists in both 'Portable.dll' and 'mscorlib.dll'.

I understand the message, but I would like to know if there is a way to use [CallerMemberName] in my portable lib ? I might have missed something.


回答1:


Use the BCL Portability Pack which provides these attributes for older versions.

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

  • ...



来源:https://stackoverflow.com/questions/16564879/using-callermembername-attribute-in-a-portable-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!