- How to perform late-bound invocation (via DispID) of COM object defined in .NET from .NET?
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Runtime.InteropServices;
- namespace TestComObject
- {
- [ComVisible(true)]
- [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
- [Guid("99476c04-574a-4271-abc8-f1d2615c3d93")]
- public interface IKnowEverything
- {
- [DispId(1030)]
- string GetTheTruth();
- }
- [ComVisible(true)]
- [Guid("9B869BAF-622A-458B-BF80-2CD6D8A3C541")]
- [ClassInterface(ClassInterfaceType.None)]
- public class AllKnowing : IKnowEverything
- {
- public string GetTheTruth()
- {
- return "I am King!";
- }
- }
- }
- static void Main(string[] args)
- {
- Guid clsid = new Guid("9B869BAF-622A-458B-BF80-2CD6D8A3C541");
- Type type = Type.GetTypeFromCLSID(clsid, true);
- object instance = Activator.CreateInstance(type);
- string methodName = "GetTheTruth";
- string methodDispIdName = "[DispID=1030]";
- string s1 = (string)type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, instance, null);
- Console.WriteLine("Via name: {0}", s1);
- string s2 = (string)type.InvokeMember(methodDispIdName, BindingFlags.InvokeMethod, null, instance, null);
- Console.WriteLine("Via DispID: {0}", s2);
- }
- // Generated .IDL file (by the OLE/COM Object Viewer)
- //
- // typelib filename: TestComObject.tlb
- [
- uuid(139DAA99-BF76-4057-BCC8-DCB35C153920),
- version(1.0),
- custom(90883F05-3D28-11D2-8F17-00A0C9A6186D, "TestComObject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=58b6bd22fbd98427")
- ]
- library TestComObject
- {
- // TLib : // TLib : mscorlib.dll : {BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}
- importlib("mscorlib.tlb");
- // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
- importlib("stdole2.tlb");
- // Forward declare all types defined in this typelib
- dispinterface IKnowEverything;
- [
- uuid(99476C04-574A-4271-ABC8-F1D2615C3D93),
- version(1.0),
- custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "TestComObject.IKnowEverything")
- ]
- dispinterface IKnowEverything {
- properties:
- methods:
- [id(0x00000406)]
- BSTR GetTheTruth();
- };
- [
- uuid(9B869BAF-622A-458B-BF80-2CD6D8A3C541),
- version(1.0),
- custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "TestComObject.AllKnowing")
- ]
- coclass AllKnowing {
- interface _Object;
- [default] dispinterface IKnowEverything;
- };
- };