40

I've many objects, each of which I have information in string about its type.
like:

string stringObjectType = "DateTime";

While running, I have no object itself.
So I can not test it typeof (object)

How can I get while running the type of the object by:

typeof (stringObjectType)
Refael
  • 6,753
  • 9
  • 35
  • 54
  • Will you have the strings as `DateTime` or `System.DateTime`? – Oscar Mederos Feb 27 '13 at 09:47
  • Is this what you need? http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx – Y2theZ Feb 27 '13 at 09:50
  • To be precise, you cannot write `typeof(object)`. You can pass a class name only. Use `object.GetType` instead. Just saying. – RoadBump Feb 27 '13 at 09:54
  • possible duplicate of [Getting a System.Type from a type name](http://stackoverflow.com/questions/179102/getting-a-system-type-from-a-type-name) – sloth Feb 27 '13 at 10:09
  • also helpfull: [Getting a fully qualified type name from a string .Net C#](http://stackoverflow.com/questions/3592706/getting-a-fully-qualified-type-name-from-a-string-net-c-sharp?rq=1) – sloth Feb 27 '13 at 10:10

2 Answers2

51
try
{
    // Get the type of a specified class.
    Type myType1 = Type.GetType("System.DateTime");
    Console.WriteLine("The full name is {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 {myType2.FullName}.");
}
catch(TypeLoadException e)
{
    Console.WriteLine(e.Message);
}
catch(Exception e)
{
    Console.WriteLine(e.Message);
}

See Type.GetType(string) on MSDN

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
HardLuck
  • 1,497
  • 1
  • 22
  • 43
  • 3
    While this code is correct. Does it answer his question? string stringObjectType = "DateTime"; will not get the correct type here. – Evelie Feb 27 '13 at 09:49
  • I believe it was only an example – animaonline Feb 27 '13 at 09:50
  • The question was about getting a type by its' name, what is wrong with my answer? – HardLuck Feb 27 '13 at 09:50
  • I see nothing wrong with you answer, you got an upvote from me ^^ – animaonline Feb 27 '13 at 09:51
  • 7
    Also, this is just copy/pasted from MSDN, without any explanation or indication of source – sloth Feb 27 '13 at 09:51
  • @DominicKexel +1. Didn't know it. – Oscar Mederos Feb 27 '13 at 09:51
  • @DominicKexel so what? It's self explanatory there are comments there. Seriously people, ease up – animaonline Feb 27 '13 at 09:52
  • @animaonline I didn't downvote, I just said it's missing any indication of source, so one could call it plagiarism. – sloth Feb 27 '13 at 09:53
  • 2
    And regardless of how correct the actual code is.. It still doesnt work with the information OP provided. – Evelie Feb 27 '13 at 09:56
  • Dont get me wrong. Im not the one downvoting here... Im just telling you its not an answer to the question at hand. – Evelie Feb 27 '13 at 09:58
  • 1
    +1 for the answer, since now the Answer has the link from MSDN as well, for `DateTime`, @HardLuck, can edit the answer with a line `Type myType1 = Type.GetType("System.DateTime");` to make it a complete answer, But I think the answer points to the core which is `Type.GetType` – Habib Feb 27 '13 at 10:00
26

You can use Type.GetType() to get a type from its string name. So you can do:

Type DateType = Type.GetType("System.DateTime");

You can't just use "DateTime" since that's not the type's name. If you do this and the name is wrong (it doesn't exist) then it'll throw an exception. So you'll need a try/catch around this.

You can get the proper type name for any given object by doing:

string TypeName = SomeObject.GetType().FullName;

If you need to use vague or incomplete names, then you're going to have a fun time messing around with reflection. Not impossible, but certainly a pain.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
PhonicUK
  • 13,486
  • 4
  • 43
  • 62