2

In order to allow compatibility with another project that is written in .Net 2.0, we have had to come up with a COM interop (the newer application is in .Net 4.0). This would work because the 2.0 process would be able to use SxS execution with .Net 4.0. In order to have a COM interop from what I understand I have to do something like this:

Type myClass = Type.GetTypeFromProgID("Net4Assembly.Assembly4");
object myInstance = Activator.CreateInstance(myClass);
IAssembly4 assembly4Interface = (IAssembly4)myInstance;
assembly4Interface.CallMethod();

I have already created the COM component and registered it and this works fine. But the problem is that since the project written in 2.0 is outside our department, I want to find a way of doing the casting in line 3 above using reflection. So far I have found a suggestion in Invoke method using Reflection on COM Object

But this doesn't work for me since when I get all the methods of the object in "myInstance" which is of type COMObject, I can only see the methods that are mentioned in that link. I get this error:

Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))

I think I should somehow cast the COMObject to the interface and then I would have access to the methods? Shouldn't I be able to extract the interface from the COMObject, then call the method using reflection? I tried GetInterfaces() from the COMObject but nothing is returned.

Community
  • 1
  • 1
Farhad Alizadeh Noori
  • 2,276
  • 17
  • 22
  • will that code work `using system.Runttime.InteropServices` in the header..? Most common problem for this `Error` is because you have `a Misspelling in the Assembly Name` that you are trying to `Invoke` – MethodMan Apr 01 '13 at 20:46
  • I am not sure whether it would work but I would try this `dynamic myInstance = Activator.CreateInstance(myClass); myInstance.CallMethod();` – I4V Apr 01 '13 at 20:52
  • Yes that's the first things I checked. The assembly name should be correct since I'm already able to call CallMethod() if I cast it to the IAssembly4 interface. I have also double checked the name of the method I'm calling(CallMethod()) using Reflection and that is correct too. I also don't get as mentioned in the link, why InvokeMember and InvokeMethod are different in this case? – Farhad Alizadeh Noori Apr 01 '13 at 20:53
  • @I4V: Unfortunately the project that has this code is using .Net 2.0 and hence no dynamic. – Farhad Alizadeh Noori Apr 01 '13 at 20:54

1 Answers1

0

I am not sure if this will work but assuming you have No Misspelled Assembly Name try something like this

Type myClass  = Type.GetTypeFromProgID("Net4Assembly.Assembly4");
object myInstance = Activator.CreateInstance(myClass );
//object[] arguments = new object[] //add parameters if youre assembly expects them here
object result = myClass .InvokeMember("SubtractTwoNumbers", BindingFlags.InvokeMethod,
     null, myInstance, arguments);
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • have you verified that the Assembly name is correct make sure the casing is exact.. I am not sure why it's not working then..this is just another way of doing what you are trying to accomplish.. `I would start simple` for example double check the name of the `assembly` if you have to `Hit F2` on the file name and copy paste it into your code that way you don't have to worry about typo's – MethodMan Apr 01 '13 at 21:15
  • As it turns out the problem was with the way I had defined the method in the interface. I had used explicit implementation which would cause the method to be not public? I used BindingFlags.NonPublic too and still couldn't reach the method ! Anyways...It is working now. Thanks a lot for your help. – Farhad Alizadeh Noori Apr 01 '13 at 21:33
  • Awesome. I am extremely happy that I was able to help I was looking at this from how I would have done things back in my `Delphi` days and doing the attempted `C# conversion` awesome I guess I have led you to the `Golden Path` two thumbs up `Farhad` – MethodMan Apr 01 '13 at 21:35