Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type.GetType() dynamic string return null

Tags:

c#

I´m using Type.GetType() to create a instance.

This works:

 var type = Type.GetType("Test.ClassServices.HowService, Test");

But, this doesn´t work. It returns null:

 var name = "How";
 var type = Type.GetType("Test.ClassServices."+name+"Service, Test");
like image 703
Fernando JS Avatar asked Apr 12 '12 15:04

Fernando JS


1 Answers

No-repro. Run this sample:

var hardCodedWorking = Type.GetType("System.String");

var stringName = "String";
var concatenatedWorking = Type.GetType("System." + stringName);

var badStringName = "string";
var concatenatedNull = Type.GetType("System." + badStringName);

From Type.GetType() on MSDN:

Gets the Type with the specified name, performing a case-sensitive search.

Based on that and my example above, I believe it's most likely that the value of name isn't matching the name of the class perfectly.

like image 80
Yuck Avatar answered Nov 02 '22 18:11

Yuck