【发布时间】:2017-08-04 17:35:58
【问题描述】:
我创建了一个.NET Standard 库,其中包含将在.NET Framework 应用程序和.NET Core 应用程序之间共享的模型。
我有一个使用DescriptionAttribute 的enum。这是.NET Standard 1.5 库中的枚举:
using System.ComponentModel;
public enum Foo
{
[Description("Description A")]
A,
[Description("Description B")]
B
}
为了能够使用DescriptionAttribute,我在 NuGet 包中添加了System.ComponentModel.Primitives。
现在在我的 .NET Framework 应用程序中,我想检索枚举的描述。
获取enum 描述的实现在.NET Core 和.NET Framework 之间是不同的。所以在我的.NET Framework 4.6.2 应用程序中,我有一个扩展GetDescription,它解析enum 的描述属性并像这样返回它:
public static string GetDescription(this Enum value)
{
Type type = value.GetType();
string name = Enum.GetName(type, value);
if (name != null)
{
FieldInfo field = type.GetField(name);
if (field != null)
{
DescriptionAttribute attr =
Attribute.GetCustomAttribute(field,
typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attr != null)
{
return attr.Description;
}
}
}
return null;
}
我得到了那个错误:
System.IO.FileNotFoundException:'无法加载文件或程序集'System.ComponentModel.Primitives,Version=4.1.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a'或其依赖项之一。系统找不到指定的文件。'
我尝试添加System.ComponentModel.Primitives,但仍然出现错误。
编辑
这是我的项目结构:
【问题讨论】:
标签: c# attributes .net-core compatibility .net-standard