Solved

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

Posted on 2004-09-23
14
194 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
send mail from app.net 8 28
comparing string in C# 3 34
c# code 19 61
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

15 Experts available now in Live!

Get 1:1 Help Now