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


C# GenericTypeParameterBuilder.MakeArrayType方法代码示例

本文整理汇总了C#中System.Reflection.Emit.GenericTypeParameterBuilder.MakeArrayType方法的典型用法代码示例。如果您正苦于以下问题:C# GenericTypeParameterBuilder.MakeArrayType方法的具体用法?C# GenericTypeParameterBuilder.MakeArrayType怎么用?C# GenericTypeParameterBuilder.MakeArrayType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Reflection.Emit.GenericTypeParameterBuilder的用法示例。


在下文中一共展示了GenericTypeParameterBuilder.MakeArrayType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.VisualBasic;

public class Example
{
    public static void Main()
    {
        // Define a dynamic assembly to contain the sample type. The
        // assembly will not be run, but only saved to disk, so
        // AssemblyBuilderAccess.Save is specified.
        //
        AppDomain myDomain = AppDomain.CurrentDomain;
        AssemblyName myAsmName = new 
            AssemblyName("MakeXxxGenericTypeParameterExample");
        AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(
            myAsmName, AssemblyBuilderAccess.Save);

        // An assembly is made up of executable modules. For a single-
        // module assembly, the module name and file name are the same 
        // as the assembly name. 
        //
        ModuleBuilder myModule = myAssembly.DefineDynamicModule(
            myAsmName.Name, myAsmName.Name + ".dll");

        // Define the sample type.
        TypeBuilder myType = myModule.DefineType("Sample", 
            TypeAttributes.Public | TypeAttributes.Abstract);

        // Make the sample type a generic type, by defining a type
        // parameter T. All type parameters are defined at the same
        // time, by passing an array containing the type parameter
        // names. 
        string[] typeParamNames = {"T"};
        GenericTypeParameterBuilder[] typeParams = 
            myType.DefineGenericParameters(typeParamNames);

        // Define a method that takes a ByRef argument of type T, a
        // pointer to type T, and one-dimensional array of type T. The
        // method returns a two-dimensional array of type T.
        //
        // To create this method, you need Type objects that represent the
        // parameter types and the return type. Use the MakeByRefType, 
        // MakePointerType, and MakeArrayType methods to create the Type
        // objects, using the generic type parameter T.
        //
        Type byRefType = typeParams[0].MakeByRefType();
        Type pointerType = typeParams[0].MakePointerType();
        Type arrayType = typeParams[0].MakeArrayType();
        Type twoDimArrayType = typeParams[0].MakeArrayType(2);

        // Create the array of parameter types.
        Type[] parameterTypes = {byRefType, pointerType, arrayType};

        // Define the abstract Test method. After you have compiled
        // and run this example code, you can use ildasm.exe to open
        // MakeXxxGenericTypeParameterExample.dll, examine the Sample
        // type, and verify the parameter types and return type of
        // the TestMethod method.
        //
        MethodBuilder myMethodBuilder = myType.DefineMethod(
            "TestMethod", 
            MethodAttributes.Abstract | MethodAttributes.Virtual
            | MethodAttributes.Public, 
            twoDimArrayType, 
            parameterTypes);

        // Create the type and save the assembly. For a single-file 
        // assembly, there is only one module to store the manifest 
        // information in.
        //
        myType.CreateType();
        myAssembly.Save(myAsmName.Name + ".dll");
    }
}
开发者ID:.NET开发者,项目名称:System.Reflection.Emit,代码行数:77,代码来源:GenericTypeParameterBuilder.MakeArrayType


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