Checking existance of method in COM object

  • Thread starter Daniel =?iso-8859-1?Q?Lidstr=F6m?=
  • Start date
D

Daniel =?iso-8859-1?Q?Lidstr=F6m?=

Hi!

I want to know if a certain member function is available in a COM object,
before I try to call it. I'm using late binding and when I know the member
exists, everything is fine. Here's the code:

Type t = Type.GetTypeFromProgID("RR_Ispol.Road");
object o = Activator.CreateInstance(t);
object[] parameters = { @"HzAlignment.txt",
@"HzAlignment.xml" };
Int32 result = (Int32)t.InvokeMember("ImportRoadHz",
BindingFlags.InvokeMethod,
null,
o,
parameters);

If the member ImportRoadHz isn't available in RR_Ispol.Road, a COMException
is thrown. How can I test if the member is there or not, before I try to
call it? Calling it and catching an exception doesn't seem to be the
optimal solution. Oh, I have to use late binding because I don't know the
interface in advance.

Thanks in advance!
 
U

UL-Tomten

If the member ImportRoadHz isn't available in RR_Ispol.Road, a COMException
is thrown. How can I test if the member is there or not, before I try to

What does t.GetMembers(...) show?
optimal solution. Oh, I have to use late binding because I don't know the
interface in advance.

Well, technically, getting the Type from the ProgID and creating an
instance of the Type could both throw, and I don't know how you'd test
those. And if you can actually instantiate a RR_Ispol.Road, isn't the
member not existing an exceptional case?
 
D

Daniel =?iso-8859-1?Q?Lidstr=F6m?=

What does t.GetMembers(...) show?

Reflection on the type shows this:

Type: System.__ComObject
Method: System.Runtime.Remoting.ObjRef CreateObjRef(System.Type)
Method: System.Object InitializeLifetimeService()
Method: System.Object GetLifetimeService()
Method: Int32 GetHashCode()
Method: Boolean Equals(System.Object)
Method: System.String ToString()
Method: System.Type GetType()
Well, technically, getting the Type from the ProgID and creating an
instance of the Type could both throw, and I don't know how you'd test
those. And if you can actually instantiate a RR_Ispol.Road, isn't the
member not existing an exceptional case?

I have received COM objects that are going to be used as plugins. The
problem is that there is no unique interface defined. Each plugin has an
interface called Road, but they're not the same. All I know is that the
plugin has a Road interface that exports 1-4 methods. I guess I have to try
them all to determine which methods are available.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top