如何通过类名获取类类型?

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

我希望我的问题清晰明了。有人能帮我吗?谢谢。

大家好,非常感谢你们的帮助。 我已经通过自己的方法解决了这个问题。

{
      Type t = Type.GetType(string.Concat(viewModel, ",", "Myspace"));
}

也许这会有所帮助:http://stackoverflow.com/questions/17045635/how-to-store-reference-to-class-property-and-access-an-instances-property-by-t - user1306322
1
尝试这种方式 string model = "Myspace.MyClass"; 使用程序集限定名称代替。 - Devesh
https://dev59.com/NnRB5IYBdhLWcg3wXWK2 - Daren Thomas
3个回答

60

只需使用typeof()函数,参数就是类名。

Type type = typeof(FIXProtoClientTest);

typeof()的MSDN页面


8
仅当类型在编译时已知。 - Alejandro

3

通常情况下,您几乎不需要进行类型比较,除非您正在使用反射或接口。尽管如此:

如果您知道要与其进行比较的类型,请使用isas运算符:

if( unknownObject is TypeIKnow ) { // run code here
< p > as运算符执行一个强制类型转换,如果失败则返回null而不是异常:

TypeIKnow typed = unknownObject as TypeIKnow;

如果您不知道类型,只需要运行时类型信息,请使用.GetType()方法:
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));

我已经修改了我的问题,使其更加清晰。你能再看一遍吗?谢谢。 - Huan Fu

3

如果您使用完全限定的类名(包括其命名空间),则您的代码行Type.GetType(model)将起作用。

此外,如果它位于与调用代码不同的程序集中,则应在引用程序集对象是该类型所在程序集的实例时使用Assembly.GetType(typeName)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接