Something to reflect upon...
-
Hi, Please consider the following code fragment in C++/CLI (I am quoting from memory )...
Assembly^ a = Assembly::LoadFile("MyTypes.dll"); Type^ myType = a->GetType("MyNamespace.MyClass"); Object^ obj = Activator::CreateInstance(myType); IMyInterface^ itf = (IMyInterface^)obj;
The last line throws an invalid cast exception even if MyClass implements IMyInterface. Any idea whats going on and how to solve this problem?SDX2000
-
Hi, Please consider the following code fragment in C++/CLI (I am quoting from memory )...
Assembly^ a = Assembly::LoadFile("MyTypes.dll"); Type^ myType = a->GetType("MyNamespace.MyClass"); Object^ obj = Activator::CreateInstance(myType); IMyInterface^ itf = (IMyInterface^)obj;
The last line throws an invalid cast exception even if MyClass implements IMyInterface. Any idea whats going on and how to solve this problem?SDX2000
Where is "IMyInterface" located? Is it in "MyTypes.dll"? If not, is the assembly containing "IMyInterface" common to the current code and the loaded assembly? Are you using a "using namespace MyTypes;" somewhere in your code?
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
Where is "IMyInterface" located? Is it in "MyTypes.dll"? If not, is the assembly containing "IMyInterface" common to the current code and the loaded assembly? Are you using a "using namespace MyTypes;" somewhere in your code?
"We make a living by what we get, we make a life by what we give." --Winston Churchill
Hi George, Thanks for responding. IMyInterface is located in the same assembly (say MyLoader) which is trying to load MyTypes.dll, in other words its not located in MyTypes.dll. A reference to MyLoader has been added while compiling MyTypes.dll. I am not using "using namespace MyTypes;" anywhere. Actually I don't want to. Doing so will defeat the purpose of interface based programming. You could think of this as a plugin based application where the main application has been written in C++/CLI and the plugins can be written in any language. Its not possible for me to add the references to all present and future plugins while compiling MyLoader. Regards, SDX.
SDX2000
-
Hi George, Thanks for responding. IMyInterface is located in the same assembly (say MyLoader) which is trying to load MyTypes.dll, in other words its not located in MyTypes.dll. A reference to MyLoader has been added while compiling MyTypes.dll. I am not using "using namespace MyTypes;" anywhere. Actually I don't want to. Doing so will defeat the purpose of interface based programming. You could think of this as a plugin based application where the main application has been written in C++/CLI and the plugins can be written in any language. Its not possible for me to add the references to all present and future plugins while compiling MyLoader. Regards, SDX.
SDX2000
Please note that two identical interfaces created in two different assemblies are not the same type. Thus, you will get casting errors. Using namespaces does not defeat the purpose of interface programming. The .NET Framework is an example of that.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
Please note that two identical interfaces created in two different assemblies are not the same type. Thus, you will get casting errors. Using namespaces does not defeat the purpose of interface programming. The .NET Framework is an example of that.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
George, I think I was bit hasty in describing my problem. I agree with you "Using namespaces does not defeat the purpose of interface programming." but that is not what I meant. Thanks for your help anyway. I have found the reason why I was getting an Invalid cast exception. An invalid cast exception may be generated due to missing assemblies! (consider Assembly1::Class1 extends Assembly2::Interface1). (Note:Assemblies can be in the same folder and still be missing!) refer suzzane cooks blog http://blogs.msdn.com/suzcook/archive/2004/06/02/debugging-an-invalidcastexception.aspx
The best way to accelerate a Macintosh is at 9.8m/sec-sec. - Marcus Dolengo