How to get class type by its class name?
Asked Answered
M

3

35
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"));
}
Materials answered 19/8, 2013 at 6:54 Comment(3)
maybe this'll help #17046135Puffin
Try this way string model = "Myspace.MyClass";Use the assembly-qualified name insteadOdyssey
#648660Sulphurize
S
61

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

Type type = typeof(FIXProtoClientTest);

MSDN on typeof()

Seroka answered 11/10, 2015 at 19:43 Comment(2)
Only if the type is known at compile-time.Borzoi
can i replace typeof for some another method?Adrianaadriane
M
3

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));
Marbles answered 19/8, 2013 at 6:59 Comment(1)
I have changed my question so that it's more clear.Could you see it again ? thxMaterials
T
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.

Trioecious answered 19/8, 2013 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.