Solved

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

Posted on 2004-09-23
14
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

635 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