How to invoke COM members

N

nano2k

Hi

I need to implement a method like this:

object InvokeCOMMember(object obj, string memberName, params object[]
parameters);

The method takes a COM class reference (obj) and tries to invoke a
member (memberName) using the specified parameters (parameters), then
returns the result back.

Thanks.
 
A

Alberto Poblacion

nano2k said:
object InvokeCOMMember(object obj, string memberName, params object[]
parameters);

The method takes a COM class reference (obj) and tries to invoke a
member (memberName) using the specified parameters (parameters), then
returns the result back.

If you are receiving "obect obj", it is not a reference to the COM class,
but to the managed wrapper that you have previously generated around the COM
class (or Visual Studio did for you when you added the Reference).
Therefore, from the point of view of your method, calling the members in obj
is just calling .Net members, regardless of whether they get later
translated into COM calls. So you should be able to call them by using
standard Reflection:

using System.Reflection;
....
private object InvokeCOMMember(object obj, string memberName, params
object[] parameters)
{
Type t = obje.GetType();
MethodInfo mi = t.GetMethod(memberName);
return mi.invoke(obj, parameters);
}
 
N

nano2k

object InvokeCOMMember(object obj, string memberName, params object[]
parameters);
The method takes a COM class reference (obj) and tries to invoke a
member (memberName) using the specified parameters (parameters), then
returns the result back.

If you are receiving "obect obj", it is not a reference to the COM class,
but to the managed wrapper that you have previously generated around the COM
class (or Visual Studio did for you when you added the Reference).
Therefore, from the point of view of your method, calling the members in obj
is just calling .Net members, regardless of whether they get later
translated into COM calls. So you should be able to call them by using
standard Reflection:

using System.Reflection;
...
private object InvokeCOMMember(object obj, string memberName, params
object[] parameters)
{
Type t = obje.GetType();
MethodInfo mi = t.GetMethod(memberName);
return mi.invoke(obj, parameters);



}- Ascunde citatul -

- Afi are text în citat -

Thank you Alberto.
Too obvious to see it :)
 

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