English 中文(简体)
AppDomain.CreateInstanceAndUnwrap fails if library s name has been changed
原标题:

Greetings,

I have an application that allows users to import libraries (.NET DLLs) they ve created, as long as the library conforms to specific guidelines I ve given them (use my namespace, decorate methods with my attribute, etc.). I copy each user lib to an internal directory and then load it into its own app domain (so user can unload it if desired). Therefore, I have the limitation that you can t load two libs with the same name.

I d like to remove this limitation without placing each user lib into a unique subdirectory of my internal directory. I tried renaming the user lib when I copy it to my internal directory. For example, if the user says to import c:SomeLib.dll and I detect I already have a lib named SomeLib.dll loaded, I copy the new file to ...MyInternalDirSomeLib2.dll. However, when I do this, my load command:

(ISomeInterface) iSomeLib = someAppDomain.CreateInstanceAndUnwrap(
                                    "SomeLib2", 
                                    "SomeNamespace.SomeClassInSomeLib");

throws an exception:

FileLoadException: Could not load file or assembly MyLib2 or one of its dependencies. The located assembly s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Is there some way to tell .NET "Ignore that fact that the file name has been changed since it was compiled"?

最佳回答

Have you considered trying the overload:

someAppDomain.CreateInstanceFrom(string assemblyFile, string typeName)

Given that you know the new dll name, since you did the rename, you can ask the App Domain to create the instance from the specified dll. That way it .Net shouldn t care whether the assembly name and the assembly filename match.

I should have prefaced this with "I haven t actually tried it", but it is just a suggestion!

问题回答

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签