指针基于类型参数

时间:2016-01-13 02:30:43

标签: c# pointers generics types

MSDN documentation of the Type.FullName property似乎表明可以使用一个指向泛型类型参数的指针的类型:

  

类型的完全限定名称,包括其命名空间,但不包括其程序集;或 null 如果当前实例表示基于类型的泛型类型参数,数组类型,指针类型 byref 类型参数,或非泛型类型定义但包含未解析类型参数的泛型类型。

(Italics mine。)

要重复,当当前实例表示基于类型参数的指针类型时,此属性将返回null

问题

  

如何获得指向泛型类型的指针的类型?

我找到了一种方法,即获取泛型类型参数,获取其类型,然后调用Type.MakePointerType

using System;

namespace ConsoleApplication {
  public class GenericClass<T> { }
  public static class Program {
    public static void Main(string[] args) {
      var type = typeof(GenericClass<>).GetGenericArguments()[0].MakePointerType();
      Console.WriteLine("Name: " + type.Name);
      Console.WriteLine("FullName: " + (type.FullName ?? "null"));
      Console.ReadKey();
    }
  }
}

/***Output***
Name: T*
FullName: null
***Output***/

还有其他(更合理的)方法吗?

0 个答案:

没有答案
相关问题