在可移植库中使用 CallerMemberName 属性 [英] Using CallerMemberName attribute in a portable library

查看:39
本文介绍了在可移植库中使用 CallerMemberName 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个面向 Windows Phone 7.1+ 和 Windows 应用商店应用(用于 WinRT)的便携式库,它使用 .net 4.5 框架.

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

我想在其中使用新的 [CallerMemberName] 属性.但是,VS2012 告诉我这个属性在我的便携式库中不可用(这看起来很正常,因为它在 WP7.1 项目中不可用).

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 { }
}

但是,一旦尝试编译它,我就会收到错误类型System.Runtime.CompilerServices.CallerMemberNameAttribute"存在于Portable.dll"和mscorlib.dll".

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'.

我理解该消息,但我想知道是否有办法在我的便携式库中使用 [CallerMemberName] ?我可能错过了什么.

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.

推荐答案

使用 BCL 便携包 为旧版本提供这些属性.

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

此包使面向 .NET Framework 4、Silverlight 4 和 5 以及 Windows Phone 7.5(包括任何可移植库组合)的项目能够使用来自更高版本 .NET 的新类型,包括:

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

  • CallerMemberNameAttribute

CallerLineNumberAttribute

CallerLineNumberAttribute

CallerFilePathAttribute

CallerFilePathAttribute

...

这篇关于在可移植库中使用 CallerMemberName 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆