c# - 在可移植库中使用 CallerMemberName 属性

标签 c# windows-runtime windows-phone

我有一个面向 Windows Phone 7.1+ 和 Windows 应用商店应用程序(适用于 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] ?我可能错过了什么。

最佳答案

使用 BCL Portability Pack它为旧版本提供了这些属性。

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

  • ...

关于c# - 在可移植库中使用 CallerMemberName 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16564879/

相关文章:

c# - 绑定(bind)到枚举

xaml - 在 XAML 中绘制元素之间的连接/路径

c# - 将数字转换为单词

c# - 项目之间的循环软依赖被认为是一个好的设计吗?

c# - 编码 UI + Windows Phone 8.1 — 获取屏幕尺寸和方向?

notifications - 如何注册 Win8 定期磁贴通知?

c# - 线程消失?

c# - 遍历动态表单对象

c# - 每次重新访问应用程序页面时,WebBrowser 控件必须重新加载内容(Windows Phone 7、Silverlight)

sql - 使用适用于 Windows Phone 的 odata 客户端库插入 Unicode 字符串