Solved

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

Posted on 2004-09-23
14
196 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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