Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Convert Type at Runtime

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
NvrBst - 25 Jan 2008 21:58 GMT
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.
Lasse Vågsæther Karlsen - 25 Jan 2008 22:13 GMT
> 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
[quoted text clipped - 14 lines]
> 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.

Signature

Lasse Vågsæther Karlsen
mailto:lasse@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3

NvrBst - 25 Jan 2008 22:57 GMT
> > 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
[quoted text clipped - 47 lines]
>
> - 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
NvrBst - 25 Jan 2008 23:24 GMT
> > > 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
[quoted text clipped - 97 lines]
>
> - 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
Lasse Vågsæther Karlsen - 26 Jan 2008 19:10 GMT
> Oww. I figured it out.  The problem was on the line
<snip solution>
> and now the convert at runtime works fine, and returns the right
> values.  Thanks :)

Glad to be of help (tongue in cheek) :)

Signature

Lasse Vågsæther Karlsen
mailto:lasse@vkarlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.