简体   繁体   English

c#获取泛型类中泛型类型参数的名称

[英]c# get the name of the generic type parameter in generic class

I'm trying to get the type parameter's name ( ClassName<TypeParameterName> ) of geneic class from the class type. 我正在尝试从类类型中获取基本类的类型参数名称( ClassName<TypeParameterName> )。

for example something like this: 例如这样的事情:

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

I searched a lot and didn't find anything about how to do this. 我搜索了很多,没有找到任何关于如何做到这一点。 the only thing I thought of was using StreamReader and read the whole .cs file and find the type there in the text. 我唯一想到的是使用StreamReader并读取整个.cs文件并在文本中找到那里的类型。 But is there any faster/cleaner way to do this? 但有没有更快/更清洁的方法来做到这一点?

Note: I'm not trying to get the type of Type1 I'm trying to get the string "Type1". 注意:我不是试图获取Type1的类型我正在尝试获取字符串“Type1”。

In your example, you already set the generic type parameter to int , so you won't get your Type1 . 在您的示例中,您已将泛型类型参数设置为int ,因此您将无法获得Type1

Try this: 尝试这个:

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

Try the following: 请尝试以下方法:

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

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

I say let's make it a nice extension method that works for all types, which seems to fit your scenario of code analysis. 我说让它成为一个适用于所有类型的好的扩展方法,这似乎适合您的代码分析场景。

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

Now typeof(MyClass<int>).GetGenericTypeParameterNames() is Type1 , while typeof(int).GetGenericTypeParameterNames() is empty. 现在typeof(MyClass<int>).GetGenericTypeParameterNames()Type1 ,而typeof(int).GetGenericTypeParameterNames()是空的。 Use .Where() with whatever criterion you please to weed out "bad" names. 使用.Where()以您喜欢的标准来清除“坏”名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM