1

Is there a way to know at runtime whether a class name is a class or not? In other words, is there some sort of method you can use that would tell you whether a string input is a .NET class?

David Klempfner
  • 8,700
  • 20
  • 73
  • 153
  • 4
    This sounds like a "[XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)", explain what you are trying to do that the ability to check if a string is a class name or not would be the solution. – Scott Chamberlain Feb 12 '14 at 05:16
  • Here is a similar question http://stackoverflow.com/questions/1827425/how-to-check-programatically-if-a-type-is-a-struct-or-a-class – TGH Feb 12 '14 at 05:17
  • You may use reflection. Start with this [related question](http://stackoverflow.com/questions/1315665/c-list-all-classes-in-assembly) – Alex R. Feb 12 '14 at 05:19

1 Answers1

5

Try this

bool isClass = false;
Type t = Type.GetType("SomeNameSpace.YourType");
if(t != null)
{
    isClass = t.IsClass;
} 

I am assuming here that your string input is the namespace/type. I am also assuming that the namespace is defined and available in the context you're running the code in. It will give you a null reference if the type is undefined.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
TGH
  • 38,769
  • 12
  • 102
  • 135