470,591 Members | 1,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

Late Binding in C#

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.
Nov 16 '05 #1
9 7472
You will need to Invoke the method. Check out Invoke in MSDN.

Thanks,

"Scott English" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.

Nov 16 '05 #2
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?

"BuddyHome" <Bu***@onlinehome.com> wrote in message
news:Og*************@TK2MSFTNGP10.phx.gbl...
You will need to Invoke the method. Check out Invoke in MSDN.

Thanks,

"Scott English" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.


Nov 16 '05 #3
Sorry Scott,

If are just trying to call a method late bound then see this article.

http://groups.google.com/groups?hl=e...%3D10%26sa%3DN

"Scott English" <no****@nospam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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?
"BuddyHome" <Bu***@onlinehome.com> wrote in message
news:Og*************@TK2MSFTNGP10.phx.gbl...
You will need to Invoke the method. Check out Invoke in MSDN.

Thanks,

"Scott English" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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.



Nov 16 '05 #4
This article is a lot better.

http://my.execpc.com/~gopalan/dotnet/reflection.html

"BuddyHome" <Bu***@onlinehome.com> wrote in message
news:uN**************@TK2MSFTNGP12.phx.gbl...
Sorry Scott,

If are just trying to call a method late bound then see this article.

http://groups.google.com/groups?hl=e...%3D10%26sa%3DN
"Scott English" <no****@nospam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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?

"BuddyHome" <Bu***@onlinehome.com> wrote in message
news:Og*************@TK2MSFTNGP10.phx.gbl...
You will need to Invoke the method. Check out Invoke in MSDN.

Thanks,

"Scott English" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
> 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.
>
>



Nov 16 '05 #5
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?

"BuddyHome" <Bu***@onlinehome.com> wrote in message
news:uN**************@TK2MSFTNGP12.phx.gbl...
Sorry Scott,

If are just trying to call a method late bound then see this article.

http://groups.google.com/groups?hl=e...%3D10%26sa%3DN
"Scott English" <no****@nospam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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?

"BuddyHome" <Bu***@onlinehome.com> wrote in message
news:Og*************@TK2MSFTNGP10.phx.gbl...
You will need to Invoke the method. Check out Invoke in MSDN.

Thanks,

"Scott English" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
> 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.
>
>



Nov 16 '05 #6
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

Nov 16 '05 #7
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" <an*******@discussions.microsoft.com> wrote in message
news:85**********************************@microsof t.com...
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 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.

Nov 16 '05 #8
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.
"Scott English" <no****@nospam.com> wrote in message
news:OL**************@TK2MSFTNGP10.phx.gbl...
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" <an*******@discussions.microsoft.com> wrote in message
news:85**********************************@microsof t.com...
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

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.



Nov 16 '05 #9

You shouldn't be programming if you can't figure this out.

Sami
www.capehill.net

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Mike MacSween | last post: by
1 post views Thread by JD Kronicz | last post: by
9 posts views Thread by Zlatko Matiś | last post: by
5 posts views Thread by eBob.com | last post: by
30 posts views Thread by lgbjr | last post: by
6 posts views Thread by Tim Roberts | last post: by
14 posts views Thread by Siv | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.