【问题标题】:How does Type T = Type.GetType("SomeClass"); give me an object?Type T = Type.GetType("SomeClass");给我一个对象?
【发布时间】:2015-09-14 07:13:39
【问题描述】:

当我进行反思时,我遇到了这个:

Type T = Type.GetType("Somenamespace.someclass");

这里的“T”是一个对象吗?如果是这样,它缺少对象的完整定义为:

Type T = new Type();

那么T怎么可能是一个对象呢?我们如何将Type.GetType("Somenamespace.someclass"); 的值分配给T

【问题讨论】:

  • 如果您是 C# 新手,您可能不想立即深入反思。

标签: c# class object types


【解决方案1】:

使用Activator.CreateInstance() 类。这样的:

var someclassType = Type.GetType(“Somenamespace.someclass”);
var someclassInstance = Activator.CreateInstance(someclassType);

还有一个例子:

using System;

class DynamicInstanceList
{
    private static string instanceSpec = "System.EventArgs;System.Random;" +
        "System.Exception;System.Object;System.Version";

    public static void Main()
    {
        string[] instances = instanceSpec.Split(';');
        Array instlist = Array.CreateInstance(typeof(object), instances.Length);
        object item;
        for (int i = 0; i < instances.Length; i++)
        {
            // create the object from the specification string
            Console.WriteLine("Creating instance of: {0}", instances[i]);
            item = Activator.CreateInstance(Type.GetType(instances[i]));
            instlist.SetValue(item, i);
        }
        Console.WriteLine("\nObjects and their default values:\n");
        foreach (object o in instlist)
        {
            Console.WriteLine("Type:     {0}\nValue:    {1}\nHashCode: {2}\n",
                o.GetType().FullName, o.ToString(), o.GetHashCode());
        }
    }
}

// This program will display output similar to the following: 
// 
// Creating instance of: System.EventArgs 
// Creating instance of: System.Random 
// Creating instance of: System.Exception 
// Creating instance of: System.Object 
// Creating instance of: System.Version 
// 
// Objects and their default values: 
// 
// Type:     System.EventArgs 
// Value:    System.EventArgs 
// HashCode: 46104728 
// 
// Type:     System.Random 
// Value:    System.Random 
// HashCode: 12289376 
// 
// Type:     System.Exception 
// Value:    System.Exception: Exception of type 'System.Exception' was thrown. 
// HashCode: 55530882 
// 
// Type:     System.Object 
// Value:    System.Object 
// HashCode: 30015890 
// 
// Type:     System.Version 
// Value:    0.0 
// HashCode: 1048575

【讨论】:

    【解决方案2】:

    TType 的对象。它不是someclass 的实例,如果您想知道的话。

    Type-class 用于描述类声明等。

    解决您的评论:

    Type T = new Type();
    

    只能被Type的其他构造函数以及继承自Type的类调用,因为parameterless Type-Constructor被声明为protected,在普通代码中调用它是没有意义的(什么它会描述吗?)。一个典型的电话会是……。像这样:

    var obj = new someclass();
    var type = obj.GetType();
    

    【讨论】:

    • @METALHEAD 在Type.GetType() 的某处还有一个new 语句(或归结为相当于实例化对象的东西)。您无需在 您的 代码中看到 new 即可获得分配给变量的对象。
    • 伙计,这个呢.......try {} catch(Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); }这里也exception e 只是在这里创建对象吗?......上面给出的解释也适用于此吗?
    • 好吧,这是另一个问题,但简短的回答是否定的:Exception e 部分简单地定义了它所定义的块应该捕获哪些异常。异常在try-block 中实例化(一个方法或一个方法调用的方法等执行此操作并抛出它,这使得它可以在 catch 块中访问。)将 throw 想象成调用一个 catch 方法。 catch 基本上就像一个嵌套的方法声明。
    【解决方案3】:

    是的,T 是 Type 类的对象,它:

    表示类型声明:类类型、接口类型、数组 类型、值类型、枚举类型、类型参数、泛型类型 定义,以及开放或封闭的构造泛型类型。 https://msdn.microsoft.com/en-us/library/System.Type%28v=vs.110%29.aspx

    Type.GetType("Somenamespace.someclass");
    

    是 Type 类的静态方法,它将返回带有与该特定类相关的属性和方法的 Type 对象。它不会是Somenamespace.someclass 的instacne。

    【讨论】:

    • 伙计,这个怎么办.......try {} catch(Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } 这里也exception e 只是在这里创建对象吗?......上面给出的解释是否也适用于此?
    猜你喜欢
    • 2011-12-31
    • 2015-03-24
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 2012-02-05
    • 2018-12-01
    • 2010-11-20
    • 2013-08-06
    相关资源
    最近更新 更多