简体   繁体   中英

Using CallerMemberName attribute in a portable library

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.

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

  • ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM