Hi everyone! I have one problem, I cannot solve. The problem is in dynamic load of one assembly from another. I am working with Visual Studio .NET and built one assembly as .exe file. The second assembly is a class library type .dll. I simply trying to load the second assembly, create object from one of the classes defined in it. The constructor of the class in the second assembly has two parameters. It looks like something like this: class MyClass { public MyClass(IMyInterfase Param1, COtherClass Param2) { …… } } Where IMyInterfase if an interface I defined in both assemblies and COtherClass is a class also defined in both assemblies. I found problem when I am calling to Activator.CreteInstance and trying to transfer in parameter array some valid values. Of cause, if I creating the object from this class in the same assembly, the result is success. Call to function Activator.CreteInstance(MyClassType, new object[2] { Param1, Param2}), where Param1 is a IMyInterfase and Param2 is an object of COtherClass class simply returns something like invalid cast exception. I tried to create object with default constructor and then call to some method, the result was the same. But if instead of my own types IMyInterfase and COtherClass I use some standard types, the result is success. May be something wrong in my types, by I cannot understand what it is. Please help me in this problem. Best regards. Igor
Igor, There may be a number of things happening here. First of all since you have defined IMyinterface and COtherClass twice (once in the exe and once in the DLL) the CLR will treat these as two separate types. If you want to use the IMyInterface and COtherClass in two assemblies directly you will need to reference them from both assemblies. For example say IMyInterface and COtherClass are defined in an assembly called shared.dll. And that you are trying to use both of them from main.exe and secondary.dll. You will want to add a reference to shared.dll to both main.exe and secondary.dll. This would mean you are statically linking to the assemblies though. That brings up a question: why are you dynamically loading an assembly? There's nothing inherently wrong in doing so, but it complicates matters considerably. A lot of people are used to being able to load and unload dlls. When you are dealing with managed code you cannot unload an assembly, once it's loaded it stays loaded until the application (or appdomain ) is terminated. Please explain why you are dynamicly loading your assembly and then we can further discuss the best way to get the behavior you are looking for. Thanks, Michael Green Microsoft Developer Support
Michael, First of all thank you your answer! I’ll try to do as you offer. The program I am writing has modular structure. That means that some modules may be installed but some not. In addition to this, the name of every module may be changed. The main program is exe and it calls to load of every module that I going to build as separate assemblies. Of cause, the every module, assembly, I am going to build so, that only one object I should create from main assembly to execute module. The interfaces, constructors, for these objects are the same. Regards.
Don't see what you're looking for? Try a search.
|