gpt4 book ai didi

c# - 实例化类会在 Assembly.GetTypes() 处产生 ReflectionTypeLoadException

转载 作者:太空宇宙 更新时间:2023-11-03 10:38:16 25 4
gpt4 key购买 nike

我发现在我的应用程序生成 Telerik 报告后

var result = new ReportProcessor().RenderReport("PDF", new InstanceReportSource { ReportDocument = new MyTelerikReport(data) }, null);
var stream = new MemoryStream(result.DocumentBytes);

return CreateHttpFileResponse("MyReport.pdf", stream, "application/pdf");

我无法获取 CurrentDomain 中的所有类型

var typesWithAttribute = (from a in AppDomain.CurrentDomain.GetAssemblies()
from t in a.GetTypes() //error appears here
//some filtering logic
select t).ToList();

我遇到了错误

System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

加载程序异常:

System.IO.FileNotFoundException: Could not load file or assembly 'DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. File name: 'DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

经过一番调查,我发现无法加载的程序集:Telerik.Reporting.OpenXmlRendering, Version=8.0.14.311, Culture=neutral, PublicKeyToken=a9d7983dfcc261be 并且该程序集不存在于 AppDomain.CurrentDomain.GetAssemblies() 在我生成报告之前(我假设程序集由 Telerik.Reporting, Version=8.0.14.311, Culture=neutral, PublicKeyToken=a9d7983dfcc261be 动态加载) .

我可以过滤掉那个程序集,因为我不需要它的任何类型,但我有点担心域中有无法加载的程序集这一事实 - 对我来说似乎有点不对劲。

有人能解释一下发生了什么吗?是我的问题还是第 3 方库没有加载所有必需程序集的错误?

最佳答案

问题不在于程序集,而是来自尚未加载的依赖程序集的 Type

If the GetTypes method is called on an assembly and a type in that assembly is dependent on a type in an assembly that has not been loaded (for example, if it derives from a type in the second assembly), a ReflectionTypeLoadException is thrown.

http://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettypes(v=vs.110).aspx

关于c# - 实例化类会在 Assembly.GetTypes() 处产生 ReflectionTypeLoadException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26591309/

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