Assembly.GetType()无法仅从ReSharper插件加载List`1 [[MyModel]]类型 [英] Assembly.GetType() fails to load List`1[[MyModel]] type from ReSharper Plugin only

查看:43
本文介绍了Assembly.GetType()无法仅从ReSharper插件加载List`1 [[MyModel]]类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个R#插件和一个测试项目(不是R#测试项目,而是一个简单的普通单元测试项目).在插件的代码中,我从序列化的类型信息中进行了一些类型解析.在这种类型上是我的一个模型类中的对象列表.该类型的序列化如下所示:

I have a R# plugin and a test project (not a R# test project, but a simple, plain unit-test project). In the code of the plugin I do some type resolution from serialized type information. On such type is a list of object from one of my model classes. The serialization of the type looks like this:

"System.Collections.Generic.List`1[[Model.Foo, Model]], mscorlib"

我通过以下调用来加载这种类型(或者实际上是Json.NET):

I load this type (or, actually, Json.NET does) with the following calls:

Assembly assembly = Assembly.LoadWithPartialName(assemblyName);
Type type = assembly.GetType(typeName);

在我的测试项目中,当我运行此代码时,一切正常. assemblyName是"mscorlib",第一行解析mscorlib程序集(作为RuntimeAssembly的实例). typeName是"System.Collections.Generic.List`1 [[Model.Foo,Model]]",对GetType()的调用将解析我的类型.

In my test project, when I run this code, everything works just fine. The assemblyName is "mscorlib" and the first line resolves the mscorlib assembly (as an instance of RuntimeAssembly). The typeName is "System.Collections.Generic.List`1[[Model.Foo, Model]]" and the call to GetType() resolves my type.

但是,当我运行R#插件时,GetType()调用将返回null.直到那行,一切都看起来像我从测试项目执行时一样.这两个项目都引用模型"程序集.当我尝试使用完全相同的机制仅解析类型"Model.Foo,Model"时,它可以同时在R#插件和测试项目中使用.

When I run the R# plugin, however, the GetType() call returns null. Till that line everything looks like when I execute from the test project. Both projects reference the "Model" assembly. When I try to resolve only the type "Model.Foo, Model" using the exact same mechanism, it works in the R# plugin and the test project both.

当我允许GetType()调用引发异常时,FileNotFound异常(当然只有在运行R#插件时才会抛出)告诉我找不到模型"程序集. stacktrace包含已扫描位置的列表.所有这些位置都在VisualStudio安装目录下.

When I allow the GetType() call to throw exception, the FileNotFound exception (of course only thrown when running the R# plugin) tells me that the "Model" assembly cannot be found. The stacktrace contains a list of locations that were scannened. All these locations are below the VisualStudio installation directory.

如上所述,上面的两行实际上是Json.NET的一部分.因此,我无法更改它们.但是,我在消除此问题的同时完全消除了Json.NET.仅上面两行,分别带有assemblyName和typeName的字符串值就足以重现该问题.

As mentioned, the two lines above are actually part of Json.NET. Therefore, I cannot change them. However, I completely eliminated Json.NET while narrowing down this problem. Just the two lines above, with the respective string values of assemblyName and typeName suffice to reproduce the problem.

有人可以告诉我这种不同的行为起源于何处吗?或者更好:告诉我如何解决这个问题?

Can anybody tell me where this different behavior originates from? Or better: tell me how to solve this problem?

提前谢谢! 最好, 斯文

Thanks in advance! Best, Sven

推荐答案

.net搜索模型程序集时,它仅查找由应用程序设置的搜索路径,即VS安装目录等.它不会在您插件的位置搜索.您可能需要向AppDomain.CurrentDomain.AssemblyResolve添加事件处理程序.检查谁在打电话以及他们要去接什么程序集,然后可以加载该程序集,也可以在内存中找到它并返回.通常,返回内存中的程序集比尝试再次加载它(由于加载上下文)要好.您可以使用AssemblyResolver类使安装和卸载处理程序变得容易,以便为您加载程序集.

When .net is searching for your Model assembly, it only looks in the search paths set up by the application, i.e. the VS install dir and so on. It doesn't search in the location of your plugin. You would probably need to add an event handler to AppDomain.CurrentDomain.AssemblyResolve. Check for who's calling, and what assembly they're after, and you can either load the assembly, or find it in memory and return it. It's generally better to return an assembly that you have in memory than to try and load it again (due to load contexts). You can use the AssemblyResolver class to make it easy to install and uninstall a handler to load the assemblies for you.

此外,基于称为模型"的事实,如果这是用于保存/加载设置,则最好使用ReSharper的设置基础结构,该基础结构提供了很大的灵活性,并消除了对AssemblyResolver.

Also, based on the fact this is called "Model", if this is for saving/loading settings, you'd be better off using ReSharper's settings infrastructure, which provides a lot of flexibility, and removes the need for the AssemblyResolver.

这篇关于Assembly.GetType()无法仅从ReSharper插件加载List`1 [[MyModel]]类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆