本文整理汇总了C#中System.Type.GetGenericTypeDefinition方法的典型用法代码示例。如果您正苦于以下问题:C# Type.GetGenericTypeDefinition方法的具体用法?C# Type.GetGenericTypeDefinition怎么用?C# Type.GetGenericTypeDefinition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.GetGenericTypeDefinition方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Reflection;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
Console.WriteLine("\r\n--- Get the generic type that defines a constructed type.");
// Create a Dictionary of Test objects, using strings for the
// keys.
Dictionary<string, Test> d = new Dictionary<string, Test>();
// Get a Type object representing the constructed type.
//
Type constructed = d.GetType();
DisplayTypeInfo(constructed);
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);
}
}
}
输出:
--- Get the generic type that defines a constructed type. System.Collections.Generic.Dictionary`2[System.String,Test] Is this a generic type definition? False Is it a generic type? True List type arguments (2): System.String Test System.Collections.Generic.Dictionary`2[TKey,TValue] Is this a generic type definition? True Is it a generic type? True List type arguments (2): TKey TValue
示例2: Type.GetGenericTypeDefinition()
//引入命名空间
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
public class MainClass
{
public static void Main()
{
Console.WriteLine(typeof(List<>).Equals(typeof(List<int>).GetGenericTypeDefinition()));
}
}