Assembly.GetType() fails to load List`1[[MyModel]] type from ReSharper Plugin only

梦想与她 提交于 2019-12-13 05:25:11

问题


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"

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

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

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.

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.

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.

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


回答1:


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.

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.



来源:https://stackoverflow.com/questions/20475345/assembly-gettype-fails-to-load-list1mymodel-type-from-resharper-plugin-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!