Solved

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

Posted on 2004-09-23
14
195 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
14 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

929 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now