Late Binding in C#

S

Scott English

I am writing an C# program. I call a method on a COM object that returns
Object. I don't know the type of the object (and reflection just says its a
__ComObject), but I know there is supposed to be Controls property. How can
I call the Controls property without knowing the type of the object?

In VB.NET, you can just do this if Option Explicit is off by just writing
SomeObject.Controls. The VB.NET runtime will handle the late binding which,
in this case, includes calling the COM object's IDispatch interface. I
don't see how to do the same thing in C#. The only "late binding" examples
I can find for C# assume you know the type of the object you are calling.
 
S

Scott English

Are you refering to MethodBase.Invoke? To get a MethodBase object, you must
first know the type of the object the method is on. Am I missing something?
 
S

Scott English

Well, in that case, you still need to know the type of the object (the
ProgID). In my case, I don't know the type of object or its ProgID, only
that it is suppose to have a "Controls" attribute.

Again, this is no problem for VB.NET. Is C# really inferior to VB.NET in
this regard? Is there really something that VB.NET can do that C# can't?
 
G

Guest

well, did you try to make the late-bound call through type System.__ComObject? according to the documentation, you should still be able to make such a call through this generic type if your object implements the IDispatch interface

----- Scott English wrote: ----

I am writing an C# program. I call a method on a COM object that return
Object. I don't know the type of the object (and reflection just says its
__ComObject), but I know there is supposed to be Controls property. How ca
I call the Controls property without knowing the type of the object

In VB.NET, you can just do this if Option Explicit is off by just writin
SomeObject.Controls. The VB.NET runtime will handle the late binding which
in this case, includes calling the COM object's IDispatch interface.
don't see how to do the same thing in C#. The only "late binding" example
I can find for C# assume you know the type of the object you are calling
 
S

Scott English

Do you mean declaring a variable as System.__ComObject? As in

__ComObject o = (__ComObject)someComObject.SomeMethod();
o.MakeLateBoundCallThroughIDispatch();

This doesn't compile. __ComObject isn't a public type. You can't declare a
variable of as __ComObject.


Daniel Jin said:
well, did you try to make the late-bound call through type
System.__ComObject? according to the documentation, you should still be
able to make such a call through this generic type if your object implements
the IDispatch interface.
 
W

Willy Denoyette [MVP]

Not sure what property you are expecting to get/set, but basically this is
what you have to do using Reflection (and what VB.NET does under the
covers).

object obj = <someCOMObject> // your __ComObject reference (Idispatch
interface)
// get property
object prop = obj.GetType().InvokeMember("", BindingFlags.GetProperty, null,
obj, null);
// cast obj to corresponding property type, fi. if a BSTR, cast object to a
string
string s = prop as string;
// set property
obj.GetType().InvokeMember("", BindingFlags.SetProperty, null, prop,
...... );

Willy.
 

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