如何避免此InvalidCastException?

时间:2017-07-24 20:09:10

标签: c# wpf

我有一个我在WinForms中使用多年的扩展方法,但是因为我尝试在新的WPF项目中使用它。方法:

public static String GetDescription(this Enum value)
{
    //var info = value.GetType().GetField(value.ToString());
    //if (info != null)
    //{
    //    var attributes = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false);
    //    if (attributes != null && attributes.Length > 0)
    //        return attributes[0].Description;
    //}
    //return value.ToString();

    var info = value.GetType().GetField(value.ToString());
    var attributes = Attribute.GetCustomAttributes(info);
    if (attributes.Length > 0 && (attributes[0] is System.ComponentModel.DescriptionAttribute))
        return ((System.ComponentModel.DescriptionAttribute)attributes[0]).Description;
    return value.ToString();
}

第一个块(已注释掉)是原始方法。第二个块是我正在测试的略有不同的版本。如果我强制执行返回行与执行,我得到这个例外:

InvalidCastException image

该对话框中的例外的全文是:

  

其他信息:[A] System.ComponentModel.DescriptionAttribute不能转换为[B] System.ComponentModel.DescriptionAttribute。类型A源自' System.ComponentModel.Primitives,Version = 4.1.1.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'在上下文中'默认'在位置' C:\ TFS_Local \ Antero \ AnteroWPF \ bin \ Debug \ System.ComponentModel.Primitives.dll'。类型B源自' System,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'在上下文中'默认'在位置' C:\ WINDOWS \ Microsoft.Net \ assembly \ GAC_MSIL \ System \ v4.0_4.0.0.0__b77a5c561934e089 \ System.dll'。

所涉及的两个议会似乎是:

System.ComponentModel.Primitives.dll(存在于构建bin目录中......不完全确定它是如何到达的)

System.dll(由于显而易见的原因,直接从GAC引用)

我完全迷失在这里。如果我删除ComponentModel DLL,那将成为例外。即使异常声称System具有该类型,但似乎不可能使用它。即在缺少相应的DLL的情况下,System.ComponentModel.DescriptionAttribute似乎不是一件有效的事情。

所以,如果我删除了一个并且那个失败了,而另一个完全无法使用......那为什么这个例外甚至会发生?!

修改 如果我在内存中检查attributes的值,我认为它没有任何价值,我看到数组确实有一个项目,它的类型为System.ComponentModel.DescriptionAttribute

2 个答案:

答案 0 :(得分:2)

  

如果我删除了ComponentModel DLL,则会成为异常

什么成为例外?

您获得的异常是100%明确的:您的项目引用的两个不同的程序集定义相同的类型System.ComponentModel.DescriptionAttribute。此类型is documented as being defined in System.dll。所以 System.ComponentModel.Primitives.dll 程序集似乎是可疑的。

通过一些网络搜索引导我进入System.ComponentModel.Primitives NuGet package。从这个描述中,这似乎与.NET Core有关,实际上,有一个implementation of that type in the .NET Core source code

在编译时,程序集似乎已放置在构建目录C:\TFS_Local\Antero\AnteroWPF\bin\Debug中。所以,问题是,如果您正在构建WPF程序,为什么在构建目录中有.NET Core程序集?你有没有安装NuGet包?您的构建目录包含根TFS_Local,它提示您可能已加入Team Foundation Server存储库。是否可能有同事为您的构建安装了包?

我没有看到任何让我相信你可以将桌面.NET Framework与.NET Core混合搭配的东西。所以简短的回答在我看来只是“不要那样做”。如果没有引用 System.ComponentModel.Primitives.dll 程序集,您可能会收到一些其他错误,但您需要以其他方式解决这些问题。我不知道有任何理由相信你应该期望能够使用.NET Core DLL编译桌面WPF程序。

如果你不相信,如果你能解释为什么你认为你可以在你的WPF程序中使用.NET Core程序集,你是如何做到这一点,以及你采取了哪些步骤来确保这些类型将会有所帮助在.NET Core中声明的与桌面.NET Framework程序集中声明的类型不冲突,包括 System.dll

答案 1 :(得分:0)

要解决此问题,请清空bin和obj文件夹。在GAC的引用中将copy local设置为false。