【问题标题】:c# get the name of the generic type parameter in generic classc#获取泛型类中泛型类型参数的名称
【发布时间】:2018-05-25 19:00:33
【问题描述】:

我正在尝试从类类型中获取泛型类的类型参数名称 (ClassName<TypeParameterName>)。

例如这样的:

class MyClass<Type1>
{
    public Type data;
}
static void Main()
{
    Console.WriteLine(typeof(MyClass<int>).GetTypeParameterName());
    //prints: Type1
}

我进行了很多搜索,但没有找到有关如何执行此操作的任何信息。 我唯一想到的是使用StreamReader 并阅读整个 .cs 文件并在文本中找到类型。但是有没有更快/更清洁的方法来做到这一点?

注意:我不是要获取 Type1 的类型,而是要获取字符串“Type1”。

【问题讨论】:

  • 你为什么要这样做?这只是一种占位符名称,你不能用这个名字做很多事情。
  • 你不是在代码中写的吗,那它已经打印出"Type1"。那么你想要什么呢? "int"?
  • @SeM 我们在我的学校学习 c#,我的学校有很多关于你应该如何写课程的规则,如果你不这样写,他们就不会'不要读它。而且我正在尝试创建一个获取类类型并返回您需要更改的类的方法(如果您违反了其中一个规则)。抱歉英语不好
  • @TᴏᴍᴇʀWᴏʟʙᴇʀɢ 他们应该给你一个 A 只是为了实现该解决方案。没有多少学生能够使用反射和元编程来实现规则检查器:-)

标签: c# generics


【解决方案1】:

在您的示例中,您已经将泛型类型参数设置为 int,因此您不会得到您的 Type1

试试这个:

class MyClass<Type1>
{
    public Type data;
}
static void Main()
{
    Console.WriteLine(typeof(MyClass<>).GetGenericArguments()[0].Name);
    //prints: Type1
}

【讨论】:

  • 请注意typeof(MyClass&lt;&gt;) == typeof(MyClass&lt;int&gt;).GetGenericTypeDefinition()
  • @JeroenMostert:所以这也适用:typeof(MyClass&lt;int&gt;).GetGenericTypeDefinition().GetGenericArguments()[0].Name;
  • 谢谢,我不知道有可能做到MyClass&lt;&gt;
【解决方案2】:

尝试以下方法:

       .
       .
       .
      //Create and object of the relevant generic class
       ClassName<string> d = new ClassName<string>();

       // Get a Type object representing the constructed type.
       Type constructed = d.GetType();

       Type generic = constructed.GetGenericTypeDefinition();
       DisplayTypeInfo(generic);
    }

    private static void DisplayTypeInfo(Type t)
    {
        Console.WriteLine("\r\n{0}", t);
        Console.WriteLine("\tIs this a generic type definition? {0}", 
            t.IsGenericTypeDefinition);
        Console.WriteLine("\tIs it a generic type? {0}", 
            t.IsGenericType);
        Type[] typeArguments = t.GetGenericArguments();
        Console.WriteLine("\tList type arguments ({0}):", typeArguments.Length);
        foreach (Type tParam in typeArguments)
        {
            Console.WriteLine("\t\t{0}", tParam);
        }
    }

来源: https://msdn.microsoft.com/en-us/library/system.type.getgenerictypedefinition(v=vs.110).aspx

【讨论】:

    【解决方案3】:

    我说让我们让它成为一个适用于所有类型的不错的扩展方法,这似乎适合您的代码分析场景。

    static class TypeExtensions {
        public static IEnumerable<string> GetGenericTypeParameterNames(this Type type) {
            if (type.IsGenericTypeDefinition) {
                return type.GetGenericArguments().Select(t => t.Name);
            } else if (type.IsGenericType) {
                return type.GetGenericTypeDefinition().GetGenericArguments().Select(t => t.Name);
            } else {
                return Enumerable.Empty<string>();
            }
        }
    }
    

    现在typeof(MyClass&lt;int&gt;).GetGenericTypeParameterNames()Type1,而typeof(int).GetGenericTypeParameterNames() 是空的。使用 .Where() 与您喜欢的任何标准来清除“坏”名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-15
      • 1970-01-01
      相关资源
      最近更新 更多