Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Getting a Type object for a user-defined class from the class's name

Posted on 2004-09-23
14
Medium Priority
?
203 Views
Last Modified: 2010-04-15
Hi,

I need to get a Type object corresponding to a user defined type that isn't known until runtime.  I know System.Type.GetType will do this for System.Types, but how about a user type?  Is there any way to do this without using the Activator class to create an instance of the object and then getting the type off of that?

This is for DeSerializing a class that isn't know until runtime.

Thanks a lot
0
Comment
Question by:bhanley9681
  • 3
  • 3
  • 2
  • +3
12 Comments
 
LVL 10

Expert Comment

by:eternal_21
ID: 12136712
What do you mean by 'User Type?'  Everything in .NET is a System.Type....
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 12136719
(or has a System.Type, I should say)
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 12136730
But, if this answers your question, you just need to call object.GetType() on the foreign object
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:tinchos
ID: 12136819
As eternal21 said

if you have

object o = new MyUserDefinedObject();

// code

you can always do

System.Type t = o.getType();

Tincho
0
 

Author Comment

by:bhanley9681
ID: 12136867
Currently, I'm trying to do:

     System.Type.GetType("MyClassHere");

This is not doing anything intelligent.  I'm passing it to the constructor of an XMLSerializer and it doesn't work.  typeof(MyClassHere) does.  "MyClassHere" is not known until runtime.

Thanks
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12136977
DataAccess.SavePref obj=new DataAccess.SavePref();
Response.Write(obj.GetType());

i could do that

it displays Dataaccess.SavePref
0
 

Author Comment

by:bhanley9681
ID: 12137089
I know you can get the type from an object w/ obj.GetType() (and I know you can create an object from a string with the type's name in it w/ the Activator), but is there some way to get the type handle w/o creating the object?
0
 
LVL 9

Expert Comment

by:tinchos
ID: 12137146
Have you tried with

System.Type t = Assembly.GetType( "MyClass" );

Tincho
0
 

Author Comment

by:bhanley9681
ID: 12137637
I got it to work.  System.Type.GetType("fully.qualified.typename") worked whereas System.Type.GetType(typename) did not.

Thanks for your help!
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 12138173


typeof("MyClass")
0
 
LVL 22

Expert Comment

by:_TAD_
ID: 12138194

whoops, scratch that... no quotes

typeof(MyClass)


for example

Type t = typeof(MyClass);
0
 

Accepted Solution

by:
RomMod earned 0 total points
ID: 12411375
The question has been PAQ'd and the 500 points have been refunded.
RomMod
Community Support Moderator
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question