【问题标题】:How do I reflect back the name of a Type supplied as a generic?如何反映作为泛型提供的类型的名称?
【发布时间】:2010-07-01 19:38:27
【问题描述】:

如何反映作为泛型参数提供的类型的名称?我真的很想知道这是如何在 C# 和 VB.NET 中完成的。请参阅以下示例代码和预期响应。

在 C# 中:

public void Test<T>()
{
     Console.WriteLine("The supplied type is {0}",???)
}

在 VB.NET 中

Public Sub Test(Of T)()
     Console.WriteLine("The supplied type is {0}",???)
End Sub

执行Test&lt;String&gt;()Test(Of String)() 应该会产生:

提供的类型是字符串

【问题讨论】:

    标签: c# .net vb.net generics reflection


    【解决方案1】:

    C#(typeof 运算符):

    public void Test<T>()
    {
        Console.WriteLine("The supplied type is {0}", typeof(T));
    }
    

    VB.NET(GetType 运算符):

    Public Sub Test(Of T)()
        Console.WriteLine("The supplied type is {0}", GetType(T))
    End Sub
    

    C# 的 typeof(T) 和 VB.NET 的 GetType(T) 返回一个 System.Type 对象,该对象具有用于检索特定 Type 信息的属性和方法。默认情况下,System.Type.ToString() 方法将返回类型的完全限定名称。要仅返回该类型的 Name,请分别在 C# 或 VB.NET 中调用 typeof(T).NameGetType(T).Name

    【讨论】:

    • 感谢您的回答!这帮助我开始了。我在System.Type 周围添加了一些附加信息,以便其他人知道它实际上是具有属性和方法的一流对象。如果我添加了任何不正确的内容,请随时对其进行编辑。
    【解决方案2】:

    Darin 的回答是正确的,但同样值得注意的是,如果您收到 T 的实例作为参数,您也可以使用 .GetType()

    public void Test<T>(T target)
    {
        Console.WriteLine("The supplied type is {0}", target.GetType());
    }
    

    只是一种不同的方法(typeof 将检查 type 参数,而 .GetType() 使用 类型的实例)。


    正如 Daniel 在 cmets 中指出的,需要考虑细微差别:typeof(T) 将返回类型参数的类型,而 .GetType() 将返回对象的确切类型——这可能继承 来自Ttypeof(T).IsAssignableFrom(target.GetType()) 可能返回true——但具体的具体类型可能不同。

    一个例子:

    using System;
    
    namespace ConsoleApplication2
    {
      class Program
      {
        static void Main(string[] args)
        {
          GenericClass<TypeX>.GenericMethod(new TypeX());
          GenericClass<TypeX>.GenericMethod(new TypeY());
          Console.ReadKey();
        }
      }
    
      public class TypeX {}
    
      public class TypeY : TypeX {}
    
      public static class GenericClass<T>
      {
        public static void GenericMethod(T target)
        {
          Console.WriteLine("TypeOf T: " + typeof(T).FullName);
          Console.WriteLine("Target Type: " + target.GetType().FullName);
          Console.WriteLine("T IsAssignable From Target Type: " + typeof(T).IsAssignableFrom(target.GetType()));
        }
      }
    }
    

    结果输出:

    传入一个TypeX的实例作为参数:
    TypeOf T:ConsoleApplication2。TypeX
    目标类型:ConsoleApplication2。TypeX
    T IsAssignable From Target Type: True

    传入 TypeY 的实例作为参数:
    TypeOf T:ConsoleApplication2。TypeX
    目标类型:ConsoleApplication2。TypeY
    T IsAssignable From Target Type: True

    【讨论】:

    • 请注意,typeof(T) 将返回指定的类型参数,而 target.GetType() 将返回所提供参数的实际类型 - 这可以是 T,也可以是 T 的每个子类型。进一步target.GetType() 可能会导致NullReferenceException
    • 我认为这个答案不值得否定。他很好地解释了不同方法的情况,这种方法可能是一种有效的做法。显然,调用 GetType() 也会导致 null ref 异常,真的不值得一提。
    • Daniel 是对的......虽然我不确定为什么提供有用信息的答案会受到反对......
    • 我完全同意,这是一个有效且有用的答案(如果有人将我的评论理解为批评,我既不赞成也不反对)。
    • @Daniel - 好点,我更新(并且可能过于复杂)我的答案以指出细微差别:D
    猜你喜欢
    • 2010-11-09
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    相关资源
    最近更新 更多