我的类库是针对 .NET 2.0 编译的,每当我尝试在 2.0 运行时下将它作为插件加载时,它都能正常工作。但是,如果主应用程序正在运行 .NET 4.0 运行时,我会在需要访问资源时立即收到异常:
Exception occurred during processing of command: Grasshopper
Plug-in = Grasshopper
Could not find file 'Grasshopper.resources'.
CALL STACK
at System.Reflection.RuntimeAssembly.InternalGetSatelliteAssembly(String name, CultureInfo culture, Version version, Boolean throwOnFileNotFound, StackCrawlMark& stackMark)
at System.Resources.ManifestBasedResourceGroveler.GetSatelliteAssembly(CultureInfo lookForCulture, StackCrawlMark& stackMark)
at System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary2 localResourceSets, Boolean tryParents, Boolean createIfNotExists, StackCrawlMark& stackMark)
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo requestedCulture, Boolean createIfNotExists, Boolean tryParents, StackCrawlMark& stackMark)
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture, Boolean wrapUnmanagedMemStream)
at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture)
at Grasshopper.My.Resources.Resources.get_GrasshopperBannerImage_Palette() in D:\dev\grasshopper\1.0\root\src\My Project\Resources.Designer.vb:line 1159
at Grasshopper.GUI.GH_Banner.ExplicitBanner_Load(Object sender, EventArgs e) in D:\dev\grasshopper\1.0\root\src\GH_Banner.vb:line 14
....
发生了什么以及如何在所有 .NET 运行时加载我的项目?
编辑:
问题是我写的一个 AssemblyResolver 在被要求加载“Grasshopper.resources”时感到困惑。似乎 .NET 4.0 处理资源加载的方式与早期运行时不同???
最佳答案
将资源程序集的测试添加到 AssemblyResolver 修复了该问题,并允许我在 .NET 2.0 和 4.0 下运行此类库:
private Assembly ResolveGrasshopper(object sender, ResolveEventArgs e)
{
if (e.Name.Contains(".resources,")) { return null; }
....
}
关于exception - .NET 2.0 与 .NET 4.0 加载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3017603/