4

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.

Matthieu Oger
  • 823
  • 1
  • 11
  • 20

1 Answers1

5

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

  • ...

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I'd rather not use an external library (especially just for that). If they managed to do that, I guess that I can too. I'm unable to find the source, so I guess it's not an open source project... :/ Thanks anyway. – Matthieu Oger May 15 '13 at 12:41
  • @Ashen: Well you can declare the attributes yourself - but then you'd need to consider all the aspects of type redirection. Microsoft has made this package available specifically to address this sort of thing. It feels like a pretty reasonable dependency to me... – Jon Skeet May 15 '13 at 13:03
  • Indeed. It adds task too, which is nice :) – Matthieu Oger May 15 '13 at 13:09