More Type/COM fun

O

oddball.bfi

Ok - now I've got me stuck again - this time good and proper.

I've got a Type object for an interface a COM object may (or may not)
implement. Lets call it IBob

Now

IBob bobObject = comObject as IBob;

if (bobObject == null)
return "Don't call me Bob";
else
return "Call me Bob! If you call me Bobby, I'll kill you";

That's all well and groovy. But my real issue starts with the fact
that I have a type object, and that don't work with 'as' as far as I
know.

if (iBobType.InstanceOf(comObject))
return "Hi - call me Bob!";
else
return "Its Robert.";

This always return Robert... COM appares to be pathalogicaly opposed
to being called Bob. What can I do?

My name is Josh, btw.
 
R

Richard Blewett

Ok - now I've got me stuck again - this time good and proper.

I've got a Type object for an interface a COM object may (or may not)
implement. Lets call it IBob

Now

IBob bobObject = comObject as IBob;

if (bobObject == null)
return "Don't call me Bob";
else
return "Call me Bob! If you call me Bobby, I'll kill you";

That's all well and groovy. But my real issue starts with the fact
that I have a type object, and that don't work with 'as' as far as I
know.

if (iBobType.InstanceOf(comObject))
return "Hi - call me Bob!";
else
return "Its Robert.";

This always return Robert... COM appares to be pathalogicaly opposed
to being called Bob. What can I do?

My name is Josh, btw.

Hi Josh,

I think you should be using IsAssignableFrom IIRC

--
Regards

Richard Blewett
DevelopMentor
http://www.dotnetconsult.co.uk/weblog2
 
O

Oddball

Hi Josh,

I think you should be using IsAssignableFrom IIRC

--
Regards

Richard Blewett
DevelopMentorhttp://www.dotnetconsult.co.uk/weblog2

That didn't work either. I swapped to IsAssignableFrom, that required
a Type. I tried comObject.GetType() but, of course, that just
returned the System.__ComObject type - which is a heap of junk, as any
good tank mechanic will tell you.

This is the last step between me and world-wide recognition for my COM
skills - at least, as far as my mates go when I sit them down and make
them hear all about my hours of hell at the hands of daemon of a model.
 
N

Nicholas Paldino [.NET/C# MVP]

Quite simply, you can't do this with the type information. COM doesn't
require that a component publish in some sort of metadata that a type
implements a particular interface or not. Because of this, with simply a
Type instance, you can't determine if a component implements a COM
interface. In COM, you have to call QueryInterface and the component will
tell you if the interface is supported or not. The catch here is that the
call to QueryInterface requires the instantiation of the component, which
means you have to move beyond the Type instance.
 
O

Oddball

Quite simply, you can't do this with the type information. COM doesn't
require that a component publish in some sort of metadata that a type
implements a particular interface or not. Because of this, with simply a
Type instance, you can't determine if a component implements a COM
interface. In COM, you have to call QueryInterface and the component will
tell you if the interface is supported or not. The catch here is that the
call to QueryInterface requires the instantiation of the component, which
means you have to move beyond the Type instance.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


That didn't work either. I swapped to IsAssignableFrom, that required
a Type. I tried comObject.GetType() but, of course, that just
returned the System.__ComObject type - which is a heap of junk, as any
good tank mechanic will tell you.
This is the last step between me and world-wide recognition for my COM
skills - at least, as far as my mates go when I sit them down and make
them hear all about my hours of hell at the hands of daemon of a model.

That's not a problem!!! How? :)
 
N

Nicholas Paldino [.NET/C# MVP]

Oddball,

Well, if you want to create an instance given a Type, you can simply
pass the Type to the static CreateInstance method of the Activator class,
and then perform your checks against the instance (obj as IWhatever).

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Oddball said:
Quite simply, you can't do this with the type information. COM
doesn't
require that a component publish in some sort of metadata that a type
implements a particular interface or not. Because of this, with simply a
Type instance, you can't determine if a component implements a COM
interface. In COM, you have to call QueryInterface and the component
will
tell you if the interface is supported or not. The catch here is that
the
call to QueryInterface requires the instantiation of the component, which
means you have to move beyond the Type instance.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Ok - now I've got me stuck again - this time good and proper.
I've got a Type object for an interface a COM object may (or may
not)
implement. Lets call it IBob

IBob bobObject = comObject as IBob;
if (bobObject == null)
return "Don't call me Bob";
else
return "Call me Bob! If you call me Bobby, I'll kill you";
That's all well and groovy. But my real issue starts with the fact
that I have a type object, and that don't work with 'as' as far as I
know.
if (iBobType.InstanceOf(comObject))
return "Hi - call me Bob!";
else
return "Its Robert.";
This always return Robert... COM appares to be pathalogicaly opposed
to being called Bob. What can I do?
My name is Josh, btw.
I think you should be using IsAssignableFrom IIRC
Richard Blewett
DevelopMentorhttp://www.dotnetconsult.co.uk/weblog2
That didn't work either. I swapped to IsAssignableFrom, that required
a Type. I tried comObject.GetType() but, of course, that just
returned the System.__ComObject type - which is a heap of junk, as any
good tank mechanic will tell you.
This is the last step between me and world-wide recognition for my COM
skills - at least, as far as my mates go when I sit them down and make
them hear all about my hours of hell at the hands of daemon of a model.

That's not a problem!!! How? :)
 

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