当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - .NET AssemblyResolve - Dynamically Loading New .dll Version

问题描述:

I have many .NET WinForm applications that use a particular .dll assembly. The company that puts out this particular product occasionally releases a new version. Since they offer strong-name assemblies, I need to download the new version of their .dll, but also go through every one of my applications and rebuild them with the reference to the new .dll file. Then, I need to deploy every one of my applications. It is a lot of time, paperwork, and testing to do this, so I asked the maker of this .dll for a workaround.

They gave me this Stack Overflow thread as a suggestion and said it would "work perfectly." However, I haven't quite figured this out.

In my application, I have added this to Program.cs:

static Program()

{

AppDomain.CurrentDomain.AssemblyResolve += delegate(object sender, ResolveEventArgs e)

{

AssemblyName requestedName = new AssemblyName(e.Name);

if (requestedName.Name.ToUpper().StartsWith("THE.PRODUCT.NAME"))

{

AssemblyName assemblyName = AssemblyName.GetAssemblyName(@"C:\The.Product.Name.15.5.0.0.dll");

return Assembly.LoadFile(@"C:\The.Product.Name.15.5.0.0.dll");

}

else

{

return null;

}

};

}

In order to get this to work, I have to remove/rename the normal product .dll file in its default location, and I put the new version in simply the root C:\ directory (as the code shows). The reason for removing the .dll from its normal location is so the "AssemblyResolve" handler fires, because the referenced assembly cannot be found.

Anyway, with the above code my application gives the following exception: "An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, ..." And so on.

What exactly am I doing wrong here? Does anyone even understand what I'm trying to accomplish? I can't believe this hasn't been done before but I am kind of at a loss at this point.

Any help would be greatly appreciated. Thank you.

分享给朋友:
您可能感兴趣的文章:
随机阅读: