当前位置: 首页>>代码示例>>C#>>正文


C# Type.IsGenericTypeDefinition属性代码示例

本文整理汇总了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));
    }
}
开发者ID:.NET开发者,项目名称:System,代码行数:63,代码来源:Type.IsGenericTypeDefinition

输出:

--- 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]);
    }
}
开发者ID:C#程序员,项目名称:System,代码行数:33,代码来源:Type.IsGenericTypeDefinition


注:本文中的System.Type.IsGenericTypeDefinition属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。