C# 在C中访问泛型类型的GenericTypeParameters#

C# 在C中访问泛型类型的GenericTypeParameters#,c#,generics,types,C#,Generics,Types,我想用它们声明的参数名生成泛型类的名称。例如,如果我有如下所示的泛型类和实例化类,我想打印“MyClass” 调试器显示genericType属性GenericTypeParameters以及所有参数名称和类型信息。但是,我无法从我的C#代码访问该集合,并将genericType强制转换为System.RuntimeType类无法工作,因为RuntimeType是内部的 那么,有没有办法以某种方式访问GenericTypeParameters属性,或者我在这里? Environment VS20

我想用它们声明的参数名生成泛型类的名称。例如,如果我有如下所示的泛型类和实例化类,我想打印
“MyClass”

调试器显示
genericType
属性GenericTypeParameters以及所有参数名称和类型信息。但是,我无法从我的C#代码访问该集合,并将
genericType
强制转换为System.RuntimeType类无法工作,因为RuntimeType是内部的

那么,有没有办法以某种方式访问GenericTypeParameters属性,或者我在这里?
Environment VS2015、.NET 4.6.1

我认为您只是在寻找它,您应该调用
genericType
而不是
type
——在这一点上,类型“参数”实际上是类型参数,因为它是一个开放类型

示例(为了简单起见,使用
字典
):

使用系统;
使用System.Collections.Generic;
课堂测试
{
静态void Main()
{
var dictionary=newdictionary();
var type=dictionary.GetType();
var genericType=type.GetGenericTypeDefinition();
foreach(genericType.GetGenericArguments()中的变量typeArgument)
{
//TKey,然后TValue
Console.WriteLine(类型参数);
}
}
}

希望有了这些信息,你可以自己算出字符串格式等。

@MichaelLiu:啊,是的-哎呀,误读了。将删除,编辑,然后取消删除。太棒了!GetGenericArguments()返回我可以在调试器中看到的GenericTypeParameters属性。混乱暂时消失了。
class MyClass<P1, M1> {}
// ... some code removed here
var myInstance = new MyClass<int,string>();
// MyClass<int,string> type info here
var type = myInstance.GetType();
 // MyClass<P1, M1> type info here
var genericType = type.GetGenericTypeDefinition();
using System;
using System.Collections.Generic;

class Test
{
    static void Main()
    {
        var dictionary = new Dictionary<string, int>();
        var type = dictionary.GetType();
        var genericType = type.GetGenericTypeDefinition();
        foreach (var typeArgument in genericType.GetGenericArguments())
        {
            // TKey, then TValue
            Console.WriteLine(typeArgument);
        }
    }
}