How to get class type by its class name?

75,430

Solution 1

just use the function typeof(). The parameter is just that class name.

Type type = typeof(FIXProtoClientTest);

MSDN on typeof()

Solution 2

Generally speaking, you'll hardly ever need to do type comparisons unless you're doing something with reflection or interfaces. Nonetheless:

If you know the type you want to compare it with, use the is or as operators:

if( unknownObject is TypeIKnow ) { // run code here

The as operator performs a cast that returns null if it fails rather than an exception:

TypeIKnow typed = unknownObject as TypeIKnow;

If you don't know the type and just want runtime type information, use the .GetType() method:

Type typeInformation = unknownObject.GetType();



     // int is a value type
    int i = 0;
    // Prints True for any value of i
    Console.WriteLine(i.GetType() == typeof(int));

    // string is a sealed reference type
    string s = "Foo";
    // Prints True for any value of s
    Console.WriteLine(s == null || s.GetType() == typeof(string));

    // object is an unsealed reference type
    object o = new FileInfo("C:\\f.txt");
    // Prints False, but could be true for some values of o
    Console.WriteLine(o == null || o.GetType() == typeof(object));

 // 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);

    // FileSystemInfo is an abstract type
    FileSystemInfo fsi = new DirectoryInfo("C:\\");
    // Prints False for all non-null values of fsi
    Console.WriteLine(fsi == null || fsi.GetType() == typeof(FileSystemInfo));

Solution 3

Your line Type.GetType(model) will work if you use the fully qualified class name, including its namespace.

Furthermore, if it's in a different assembly from the code that makes the call you should use Assembly.GetType(typeName) when the assembly object referred to is an instance of the assembly containing the type.

Share:
75,430

Related videos on Youtube

Huan Fu
Author by

Huan Fu

Updated on May 06, 2020

Comments

  • Huan Fu
    Huan Fu about 4 years
    namespace Myspace
    {
        public class MyClass
        {
        }
    } //This class is in another file.
    
    using Myspace;
    static void Main(string[] args)
    {
        Regex regexViewModelKey = new Regex(RegularExpr.ViewModelKeyPattern);
        string viewModel = regexViewModelKey.Match(match.Value).Value;
        //Now, vieModel is a string, and its value is "MyClass". So, I only know the class name, this is why I ask this question.
    
        //Now, I'm only allowed to use the string form of this class name to get its type.
        //I have tyied like this, but its no use.
        Type t = Type.GetType(viewModel);
        //it's return a null.
    
        //Then I tyied another method like this, but there is an exception when calling Assembly.Load
        Assembly assembly = Assembly.Load("Myspace");
        Type ty = assembly.GetType("Myspace" + viewModel);
    }
    

    I hope my question is clear. Can any one help me.THX I'm only allowed to use the string form of this class name to get its type.

    thx everyone. I have solved this question by myself like this.

    {
          Type t = Type.GetType(string.Concat(viewModel, ",", "Myspace"));
    }
    
  • Huan Fu
    Huan Fu almost 11 years
    I have changed my question so that it's more clear.Could you see it again ? thx
  • Alejandro
    Alejandro over 7 years
    Only if the type is known at compile-time.