本文整理汇总了C#中System.Type.IsGenericType属性的典型用法代码示例。如果您正苦于以下问题:C# Type.IsGenericType属性的具体用法?C# Type.IsGenericType怎么用?C# Type.IsGenericType使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Type
的用法示例。
在下文中一共展示了Type.IsGenericType属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Reflection;
public class Base<T, U> {}
public class Derived<V> : Base<string, V>
{
public G<Derived <V>> F;
public class Nested {}
}
public class G<T> {}
class Example
{
public static void Main()
{
// Get the generic type definition for Derived, and the base
// type for Derived.
//
Type tDerived = typeof(Derived<>);
Type tDerivedBase = tDerived.BaseType;
// Declare an array of Derived<int>, and get its type.
//
Derived<int>[] d = new Derived<int>[0];
Type tDerivedArray = d.GetType();
// Get a generic type parameter, the type of a field, and a
// type that is nested in Derived. Notice that in order to
// get the nested type it is necessary to either (1) specify
// the generic type definition Derived<>, as shown here,
// or (2) specify a type parameter for Derived.
//
Type tT = typeof(Base<,>).GetGenericArguments()[0];
Type tF = tDerived.GetField("F").FieldType;
Type tNested = typeof(Derived<>.Nested);
DisplayGenericType(tDerived, "Derived<V>");
DisplayGenericType(tDerivedBase, "Base type of Derived<V>");
DisplayGenericType(tDerivedArray, "Array of Derived<int>");
DisplayGenericType(tT, "Type parameter T from Base<T>");
DisplayGenericType(tF, "Field type, G<Derived<V>>");
DisplayGenericType(tNested, "Nested type in Derived<V>");
}
public static void DisplayGenericType(Type t, string caption)
{
Console.WriteLine("\n{0}", caption);
Console.WriteLine(" Type: {0}", t);
Console.WriteLine("\t IsGenericType: {0}",
t.IsGenericType);
Console.WriteLine("\t IsGenericTypeDefinition: {0}",
t.IsGenericTypeDefinition);
Console.WriteLine("\tContainsGenericParameters: {0}",
t.ContainsGenericParameters);
Console.WriteLine("\t IsGenericParameter: {0}",
t.IsGenericParameter);
}
}
输出:
DerivedType: Derived`1[V] IsGenericType: True IsGenericTypeDefinition: True ContainsGenericParameters: True IsGenericParameter: False Base type of Derived Type: Base`2[System.String,V] IsGenericType: True IsGenericTypeDefinition: False ContainsGenericParameters: True IsGenericParameter: False Array of Derived Type: Derived`1[System.Int32][] IsGenericType: False IsGenericTypeDefinition: False ContainsGenericParameters: False IsGenericParameter: False Type parameter T from Base Type: T IsGenericType: False IsGenericTypeDefinition: False ContainsGenericParameters: True IsGenericParameter: True Field type, G > Type: G`1[Derived`1[V]] IsGenericType: True IsGenericTypeDefinition: False ContainsGenericParameters: True IsGenericParameter: False Nested type in Derived Type: Derived`1+Nested[V] IsGenericType: True IsGenericTypeDefinition: True ContainsGenericParameters: True IsGenericParameter: False
示例2:
public class Base<T, U> {}
public class Derived<V> : Base<string, V>
{
public G<Derived <V>> F;
public class Nested {}
}
public class G<T> {}