473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Type.GetType("ObjectName");

Jon
Hello all,

I'm trying to do the above, pass in an object name and it return the
type, but it's erroring, I've seen something like this before but
can't quite remember. Can this be done?

Thanks

Jon

May 6 '07 #1
4 1344
The following is lifted verbatim from the MSDN Documentation. Hopefully it
will help clear things up:

using System;
namespace MyTypeNameSpace
{
class MyClass
{
public static void Main(string[] arg)
{
try
{
// Get the type of a specified class.
Type myType1 = Type.GetType("System.Int32");
Console.WriteLine("The full name is {0}.", myType1.FullName);
// Since NoneSuch does not exist in this assembly, GetType
throws a TypeLoadException.
Type myType2 = Type.GetType("NoneSuch", true);
Console.WriteLine("The full name is {0}.", myType2.FullName);
}
catch(TypeLoadException e)
{
Console.WriteLine(e.Message);
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Jon" wrote:
Hello all,

I'm trying to do the above, pass in an object name and it return the
type, but it's erroring, I've seen something like this before but
can't quite remember. Can this be done?

Thanks

Jon

May 6 '07 #2
I'm trying to do the above, pass in an object name and it return the
type, but it's erroring, I've seen something like this before but
can't quite remember. Can this be done?
What's the error?
Unable to find the type? Check that it's the correct assembly from where you
are loading...
--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujini-labs.com
-----------------------------------------
May 6 '07 #3
On May 6, 2:17 pm, Jon <JonMYa...@gmail.comwrote:
Hello all,

I'm trying to do the above, pass in an object name and it return the
type, but it's erroring, I've seen something like this before but
can't quite remember. Can this be done?

Thanks

Jon
hi , what is your environment ? are you doing it in asp.net and your
cusotm objects if yes, the problem is that this object lives in
another assembly whose name you do not know, if this is the this is a
workaround, it works only if all classes are in same directory
(app_code) which will be compiled to same dll

//
Type rootType = typeof (SomeClassFromAppCode);
string fullAssemblyName = rootType.Assembly.FullName;

string fullClassName = typeName +", " + fullAssemblyName;
Type type = Type.GetType(fullClassName);
May 7 '07 #4
Type rootType = typeof (SomeClassFromAppCode);
string fullAssemblyName = rootType.Assembly.FullName;

string fullClassName = typeName +", " + fullAssemblyName;
Type type = Type.GetType(fullClassName);

Better use:

rootType.Assembly.GetType(typeName)

The code written above will be slower because of unnecessary overheads.
--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujini-labs.com
http://eduzine.edujini-labs.com
-----------------------------------------
May 8 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: darrell | last post by:
I recently wrote a program in Access VBA that contains this snippet of code: For i = 1 to TotalBanks With Me("txtBank" & Chr(i + 64)) .BackColor = vbBlue .ForeColor = vbWhite End With Next i...
5
by: Lucas Prado Melo | last post by:
Hello, How could I "prove" to someone that python accepts this syntax using the documentation (I couldn't find it anywhere): classname.functionname(objectname)
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.