當前位置: 首頁>>代碼示例>>C#>>正文


C# GenericTypeParameterBuilder.MakeByRefType方法代碼示例

本文整理匯總了C#中System.Reflection.Emit.GenericTypeParameterBuilder.MakeByRefType方法的典型用法代碼示例。如果您正苦於以下問題:C# GenericTypeParameterBuilder.MakeByRefType方法的具體用法?C# GenericTypeParameterBuilder.MakeByRefType怎麽用?C# GenericTypeParameterBuilder.MakeByRefType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Reflection.Emit.GenericTypeParameterBuilder的用法示例。


在下文中一共展示了GenericTypeParameterBuilder.MakeByRefType方法的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.MakeByRefType


注:本文中的System.Reflection.Emit.GenericTypeParameterBuilder.MakeByRefType方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。