gpt4 book ai didi

c# - 我怎样才能避免这个 InvalidCastException?

转载 作者:行者123 更新时间:2023-11-30 16:43:15 25 4
gpt4 key购买 nike

我有一个扩展方法,我在 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();
}

第一个 block (已注释掉)是原始方法。第二个 block 是我一直在测试的它的一个稍微不同的版本。如果我强制执行带有强制转换的返回行,我会得到这个异常:

InvalidCastException image

该对话框中异常的全文是:

Additional information: [A]System.ComponentModel.DescriptionAttribute cannot be cast to [B]System.ComponentModel.DescriptionAttribute. Type A originates from 'System.ComponentModel.Primitives, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' in the context 'Default' at location 'C:\TFS_Local\Antero\AnteroWPF\bin\Debug\System.ComponentModel.Primitives.dll'. Type B originates from 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'Default' at location 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll'.

涉及的两个程序集似乎是:

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

System.dll(直接从 GAC 引用,原因很明显)

我完全迷失在这里。如果我删除 ComponentModel DLL,那将成为异常。尽管异常声称 System 具有该类型,但似乎无法使用它。 IE。 System.ComponentModel.DescriptionAttribute 在没有相应的 DLL 的情况下似乎不是一件有效的事情。

因此,如果我删除一个失败,而另一个完全无法使用...那么为什么会出现此异常?!

编辑:我认为如果我检查内存中 attributes 的值,我会发现数组确实有一个项目并且它的类型是 System.ComponentModel.DescriptionAttribute .

最佳答案

If I remove the ComponentModel DLL, that becomes the exception

什么成为异常(exception)?

您得到的异常是 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

关于c# - 我怎样才能避免这个 InvalidCastException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45289207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com