Convert Type at Runtime

N

NvrBst

I have a COM object with a RCW avalible via Web Service. My RCW
Interop dll is in my bin directory labled "Interop.ASMLib.dlI", and
"using ASMLib;" is at the top of my class. I can call any method like
so.

using ASMLib;
A1 - myClass o1 = (myClass)Server.CreateObject("ASM.myClass");
A2 - string ret = o1.get_Test(1, 1);

This works perfectly, however, I have a string s1 (IE s1 = "myClass"
in this example) avalible at runtime and would like to convert the
"Server.CreateObject(...)" object to the type labled in s1. I've
tried

B1 - object o1 = Server.CreateObject("ASM.myClass");
B2 - object o2 = Convert.ChangeType(o1, Type.GetType("ASMLib.myClass,
Interop.ASMLib, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null"));

It throws excpetion "System.InvalidCastException: Object must
implement IConvertible."

Since Line A1 and A2 work fine I'm sure there has to be a way I can
convert the Object to "myClass" at runtime without changing the COM
object? I got the AssemblyQualifiedName by echoing
"o1.AssemblyQualifiedName" in the Line A1 example, then using it in
the Lind B2 example. If anyone has some suggestions or explainations
I'd be very greatful :)


Thanks
NB

PS: I have a slight workaround which is just a huge switch() statment
that calls each COM method as labled in the A1A2 example using the
information I have at runtime (strings of class / method / params),
however, this is ~50 lines (a lot of types) while if I could convert
then invoke, it would reduce down to just a couple lines.
 
L

Lasse Vågsæther Karlsen

NvrBst said:
I have a COM object with a RCW avalible via Web Service. My RCW
Interop dll is in my bin directory labled "Interop.ASMLib.dlI", and
"using ASMLib;" is at the top of my class. I can call any method like
so.

using ASMLib;
A1 - myClass o1 = (myClass)Server.CreateObject("ASM.myClass");
A2 - string ret = o1.get_Test(1, 1);

This works perfectly, however, I have a string s1 (IE s1 = "myClass"
in this example) avalible at runtime and would like to convert the
"Server.CreateObject(...)" object to the type labled in s1. I've
tried

B1 - object o1 = Server.CreateObject("ASM.myClass");
B2 - object o2 = Convert.ChangeType(o1, Type.GetType("ASMLib.myClass,
Interop.ASMLib, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null"));

If the object returned is really a myClass, do you really have to
convert? Typecasting serves two possible functions:

1. allowing you to store a value you know is of a particular type in a
variable of that type
2. convert from one type to another

It's the second one you're getting the error message about IConvertible
from.

But, if the object you get back is really of type "ASM.myClass", why do
you have to convert it at all? Can't you simply store it in a Object
variable directly?
Since Line A1 and A2 work fine I'm sure there has to be a way I can
convert the Object to "myClass" at runtime without changing the COM

As noted, I think it already is of type myClass.
PS: I have a slight workaround which is just a huge switch() statment
that calls each COM method as labled in the A1A2 example using the

I think the main problem here (for us) is that you're rather vague on
*why* you want to "convert" it. Perhaps some more information would help
us help you.
 
N

NvrBst

If the object returned is really a myClass, do you really have to
convert? Typecasting serves two possible functions:

1. allowing you to store a value you know is of a particular type in a
variable of that type
2. convert from one type to another

It's the second one you're getting the error message about IConvertible
from.

But, if the object you get back is really of type "ASM.myClass", why do
you have to convert it at all? Can't you simply store it in a Object
variable directly?


As noted, I think it already is of type myClass.


I think the main problem here (for us) is that you're rather vague on
*why* you want to "convert" it. Perhaps some more information would help
us help you.

--
Lasse Vågsæther Karlsen
mailto:[email protected]://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3- Hide quoted text -

- Show quoted text -

Ahh sorry about being Vague, wasn't my intention :) Basically this is
a Website (web service), so not a normal windows form application.
The COM object is an ATL ActiveX EXE, designed for ASP. The site is
in C# ASP.NET. I posted here because I think the converting problem I
have is a C# problem and not exclusive to Web Designer.

In order for the ASP intrinsics to be correctly applied, when using
ASP.NET, I believe "Server.CreateObject(...)" is the only option
(which returns type "object").

For example, if I did "ASM.myClass TEST = new ASM.myClass();" then
TEST.get_TEST(1,1); always returns null. This is true when using
Activator's "CreateInstance" methods as well. The reasion for this is
because the .aspx pages all extend the Web.UI.Page class, and I
believe Server.CreateObject auto sets up a bunch of stuff which the
other 2 methods do not (for example calls the OnPageStart method found
in the COM Object, kinda like a constructor). I've tried manually
doing the setup in the past (using new ASM.myClass(); - to avoid the
late binding penalties) but ran into some problems with what some of
the parameters are.

The RCW layer of the COM object has 3 entries for each class. IE
"myClass" has "ImyClass" "myClass" "myClassClass". I when I do

myClass o1 = (myClass)Server.CreateObject("ASM.myClass");
o1.GetType().AssemblyQualifiedName.

Then I get the "___COM Object ..." Name. ~~ I had to do "(new
myClass).GetType().AssemblyQualifedName" to get the name that I'm
trying to convert o1 to, the one that it throws the Exception On ~~

I think maybe when I do (myClass)Server.CreateObject("ASM.myClass");
its actually seeing the interface "ImyClass", and using that. When I
do

ImyClass o1 = (ImyClass)Server.CreateObject("ASM.myClass");
o1.get_Test(1,1);

It works fine. I can't seem to get the AssemblyQualifiedName for the
"ImyClass" though (using "ASMLib.ImyClass, Interop.ASMLib,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") with the
Convert function says it can't find the class.

If there is anything more specific I would be more than happy to try
and provide the information :)

NB
 
N

NvrBst

Ahh sorry about being Vague, wasn't my intention :)  Basically this is
a Website (web service), so not a normal windows form application.
The COM object is an ATL ActiveX EXE, designed for ASP.  The site is
in C# ASP.NET.  I posted here because I think the converting problem I
have is a C# problem and not exclusive to Web Designer.

In order for the ASP intrinsics to be correctly applied, when using
ASP.NET, I believe "Server.CreateObject(...)" is the only option
(which returns type "object").

For example, if I did "ASM.myClass TEST = new ASM.myClass();" then
TEST.get_TEST(1,1); always returns null.  This is true when using
Activator's "CreateInstance" methods as well.  The reasion for this is
because the .aspx pages all extend the Web.UI.Page class, and I
believe Server.CreateObject auto sets up a bunch of stuff which the
other 2 methods do not (for example calls the OnPageStart method found
in the COM Object, kinda like a constructor).  I've tried manually
doing the setup in the past (using new ASM.myClass(); - to avoid the
late binding penalties) but ran into some problems with what some of
the parameters are.

The RCW layer of the COM object has 3 entries for each class.  IE
"myClass" has "ImyClass" "myClass" "myClassClass".  I when I do

myClass o1 = (myClass)Server.CreateObject("ASM.myClass");
o1.GetType().AssemblyQualifiedName.

Then I get the "___COM Object ..." Name.  ~~ I had to do "(new
myClass).GetType().AssemblyQualifedName" to get the name that I'm
trying to convert o1 to, the one that it throws the Exception On ~~

I think maybe when I do (myClass)Server.CreateObject("ASM.myClass");
its actually seeing the interface "ImyClass", and using that.  When I
do

ImyClass o1 = (ImyClass)Server.CreateObject("ASM.myClass");
o1.get_Test(1,1);

It works fine.  I can't seem to get the AssemblyQualifiedName for the
"ImyClass" though (using "ASMLib.ImyClass, Interop.ASMLib,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null") with the
Convert function says it can't find the class.

If there is anything more specific I would be more than happy to try
and provide the information :)

NB- Hide quoted text -

- Show quoted text -

Oww. I figured it out. The problem was on the line

myClass o1 = (myClass)Server.CreateObject("ASM.myClass");

ASM.myClass was the actual COM object, while "myClass o1" is of type
"ASMLib.myClassClass", which is why it couldn't convert. Reasion it
worked must of been because it seen the interface "ImyClass" and used
that somehow?

I changed it to...

Type t1 = Type.GetType("ASMLib.myClassClass, Interop.ASMLib,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
object o1 = Server.CreateObject(t1);

and now the convert at runtime works fine, and returns the right
values. Thanks :)

NB
 

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

Similar Threads


Top