开发者

Using Type.GetType() with unreferenced assembly with Cassini/Visual Studio Dev. Server

I've got some code

var type = Type.GetType("namespace, assembly");
return Activator.CreateInstance(type);

thats work开发者_开发问答s fine in most situations, however when this code is referenced in the Global.asax of a website that is debugged using Cassini/Visual Studio Development Server the type cannot be found.

The type is in an assembly that is not referenced but is normally in the same output directory as the executing assembly. However upon closer inspection it appears that during debugging Cassini put the executing assembly and each referenced assembly into its own directory in the following location C:\users...\AppData\Local\Temp\Temporary ASP.NET Files\root..... however obviously the 'unreferenced' assembly is not there.

Is there a way to have VS copy extra resources into the temp directory or alternatively to run from a specified directory? Is using IIS the only solution?

Thanks in advance.


I think that this is not Cassini's doing, this is ASP.NET using .NET shadow copy to prevent locking your DLLs. So, using IIS should not change anything. The lack of explicit reference seems to let .NET put the DLLs in different directories.

Can you put this referenced DLL in the GAC ?


This is in response to your comment on the first answer ("Does anyone have any other possible solutions?")...

Yes, there is a very simple solution and it comes from the same source that is causing the "problem" (spoiler...its the System.AppDomain). There is an event called "AssemblyResolve" in the AppDomain class that you can respond to (Use AppDomain.CurrentDomain to get "your" AppDomain instance). The event will provide you with the name (string) of the assembly it cannot find. I assume you do know the location of the assemblies in question, so just use System.Reflection.Assembly.Load(pathToYourAssembly) to load the assembly and then return the Assembly instance that the "Load" methods returns. The "AssemblyResolve" event handler is a bit different. It has a return type, so in your event handler you will do a "return thisAssembly;" where "thisAssembly" is the return value of the Load method. The AppDomain also has a TypeResolve event (and others) in case it can't find a type in the assembly its "supposed to be in". This can happen if you have moved a type from one assembly to another and not recompiled everything else that referenced that type. Anyway, hope that helps someone. I know this is an old question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜