• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

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

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
bhanley9681
Asked:
bhanley9681
  • 3
  • 3
  • 2
  • +3
1 Solution
 
eternal_21Commented:
What do you mean by 'User Type?'  Everything in .NET is a System.Type....
0
 
eternal_21Commented:
(or has a System.Type, I should say)
0
 
eternal_21Commented:
But, if this answers your question, you just need to call object.GetType() on the foreign object
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
tinchosCommented:
As eternal21 said

if you have

object o = new MyUserDefinedObject();

// code

you can always do

System.Type t = o.getType();

Tincho
0
 
bhanley9681Author Commented:
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
 
praneethaCommented:
DataAccess.SavePref obj=new DataAccess.SavePref();
Response.Write(obj.GetType());

i could do that

it displays Dataaccess.SavePref
0
 
bhanley9681Author Commented:
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
 
tinchosCommented:
Have you tried with

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

Tincho
0
 
bhanley9681Author Commented:
I got it to work.  System.Type.GetType("fully.qualified.typename") worked whereas System.Type.GetType(typename) did not.

Thanks for your help!
0
 
_TAD_Commented:


typeof("MyClass")
0
 
_TAD_Commented:

whoops, scratch that... no quotes

typeof(MyClass)


for example

Type t = typeof(MyClass);
0
 
RomModCommented:
The question has been PAQ'd and the 500 points have been refunded.
RomMod
Community Support Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now