本文整理汇总了C#中System.Type.IsGenericTypeDefinition属性的典型用法代码示例。如果您正苦于以下问题:C# Type.IsGenericTypeDefinition属性的具体用法?C# Type.IsGenericTypeDefinition怎么用?C# Type.IsGenericTypeDefinition使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Type
的用法示例。
在下文中一共展示了Type.IsGenericTypeDefinition属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DisplayGenericTypeInfo
//引入命名空间
using System;
using System.Reflection;
using System.Collections.Generic;
public class Test
{
private static void DisplayGenericTypeInfo(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);
if (t.IsGenericType)
{
// If this is a generic type, display the type arguments.
//
Type[] typeArguments = t.GetGenericArguments();
Console.WriteLine("\tList type arguments ({0}):",
typeArguments.Length);
foreach (Type tParam in typeArguments)
{
// If this is a type parameter, display its
// position.
//
if (tParam.IsGenericParameter)
{
Console.WriteLine("\t\t{0}\t(unassigned - parameter position {1})",
tParam,
tParam.GenericParameterPosition);
}
else
{
Console.WriteLine("\t\t{0}", tParam);
}
}
}
}
public static void Main()
{
Console.WriteLine("\r\n--- Display information about a constructed type, its");
Console.WriteLine(" generic type definition, and an ordinary type.");
// Create a Dictionary of Test objects, using strings for the
// keys.
Dictionary<string, Test> d = new Dictionary<string, Test>();
// Display information for the constructed type and its generic
// type definition.
DisplayGenericTypeInfo(d.GetType());
DisplayGenericTypeInfo(d.GetType().GetGenericTypeDefinition());
// Display information for an ordinary type.
DisplayGenericTypeInfo(typeof(string));
}
}
输出:
--- Display information about a constructed type, its generic type definition, and an ordinary type. System.Collections.Generic.Dictionary[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[TKey,TValue] Is this a generic type definition? True Is it a generic type? True List type arguments (2): TKey (unassigned - parameter position 0) TValue (unassigned - parameter position 1) System.String Is this a generic type definition? False Is it a generic type? False
示例2: Main
//引入命名空间
using System;
using System.Reflection;
public class MyClass<T, V> {
public T membera;
}
public class XClass {
public void MethodA<T>() {
}
}
class Starter {
static void Main() {
Type[] types = { typeof(MyClass<,>), typeof(MyClass<int, int>) };
bool[,] bresp = { {types[0].IsGenericType,
types[0].IsGenericTypeDefinition},
{types[1].IsGenericType,
types[1].IsGenericTypeDefinition}};
Console.WriteLine("Is MyClass<,> a generic type? " + bresp[0, 0]);
Console.WriteLine("Is MyClass<,> open? " + bresp[0, 1]);
Console.WriteLine("Is MyClass<int,int> a generic type? " + bresp[1, 0]);
Console.WriteLine("Is MyClass<int,int> open? " + bresp[1, 1]);
Type tObj = typeof(XClass);
MethodInfo method = tObj.GetMethod("MethodA");
bool[] bMethod ={method.IsGenericMethod, method.IsGenericMethodDefinition};
Console.WriteLine("Is XClass.MethodA<T> a generic method? " + bMethod[0]);
Console.WriteLine("Is XClass.MethodA<T> open? " + bMethod[1]);
}
}