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.

4
  • 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 Commented Apr 1, 2013 at 20:46
  • I am not sure whether it would work but I would try this dynamic myInstance = Activator.CreateInstance(myClass); myInstance.CallMethod(); Commented Apr 1, 2013 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? Commented Apr 1, 2013 at 20:53
  • @I4V: Unfortunately the project that has this code is using .Net 2.0 and hence no dynamic. Commented Apr 1, 2013 at 20:54

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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
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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.