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


C# TypeBuilder.DefineMethod方法代码示例

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


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

示例1:

// Define a Shared, Public method with standard calling
// conventions. Do not specify the parameter types or the
// return type, because type parameters will be used for 
// those types, and the type parameters have not been
// defined yet.
MethodBuilder demoMethod = demoType.DefineMethod(
    "DemoMethod", 
    MethodAttributes.Public | MethodAttributes.Static
);
开发者ID:.NET开发者,项目名称:System.Reflection.Emit,代码行数:9,代码来源:TypeBuilder.DefineMethod

示例2:

// Defining generic parameters for the method makes it a
// generic method. By convention, type parameters are 
// single alphabetic characters. T and U are used here.
//
string[] typeParamNames = {"T", "U"};
GenericTypeParameterBuilder[] typeParameters = 
    demoMethod.DefineGenericParameters(typeParamNames);

// The second type parameter is constrained to be a 
// reference type.
typeParameters[1].SetGenericParameterAttributes( 
    GenericParameterAttributes.ReferenceTypeConstraint);
开发者ID:.NET开发者,项目名称:System.Reflection.Emit,代码行数:12,代码来源:TypeBuilder.DefineMethod

示例3:

// Set parameter types for the method. The method takes
// one parameter, and its type is specified by the first
// type parameter, T.
Type[] parms = {typeParameters[0]};
demoMethod.SetParameters(parms);

// Set the return type for the method. The return type is
// specified by the second type parameter, U.
demoMethod.SetReturnType(typeParameters[1]);
开发者ID:.NET开发者,项目名称:System.Reflection.Emit,代码行数:9,代码来源:TypeBuilder.DefineMethod

示例4: HelloMethod

//引入命名空间
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;

public interface IMyInterface
{
   String HelloMethod(String parameter);
}

public class Example
{
   [PermissionSetAttribute(SecurityAction.Demand, Name="FullTrust")]
   public static void Main()
   {
      Type myNestedClassType = CreateCallee(Thread.GetDomain());
      // Cretae an instance of 'MyNestedClass'.
      IMyInterface myInterface =
         (IMyInterface)Activator.CreateInstance(myNestedClassType);
      Console.WriteLine(myInterface.HelloMethod("Bill"));
   }

   // Create the callee transient dynamic assembly.
   private static Type CreateCallee(AppDomain myAppDomain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "Example";
      // Create the callee dynamic assembly.
      AssemblyBuilder myAssembly =
         myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
      // Create a dynamic module in the callee assembly.
      ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
      // Define a public class named "MyHelloWorld".
      TypeBuilder myHelloWorldType =
         myModule.DefineType("MyHelloWorld", TypeAttributes.Public);
      // Define a public nested class named 'MyNestedClass'.
      TypeBuilder myNestedClassType =
         myHelloWorldType.DefineNestedType("MyNestedClass",
            TypeAttributes.NestedPublic, typeof(Example),
            new Type[]{typeof(IMyInterface)});
      // Implement 'IMyInterface' interface.
      myNestedClassType.AddInterfaceImplementation(typeof(IMyInterface));
      // Define 'HelloMethod' of 'IMyInterface'.
      MethodBuilder myHelloMethod =
         myNestedClassType.DefineMethod("HelloMethod",
            MethodAttributes.Public | MethodAttributes.Virtual,
            typeof(String), new Type[]{typeof(String)});
      // Generate IL for 'GetGreeting' method.
      ILGenerator myMethodIL = myHelloMethod.GetILGenerator();
      myMethodIL.Emit(OpCodes.Ldstr, "Hi! ");
      myMethodIL.Emit(OpCodes.Ldarg_1);
      MethodInfo infoMethod =
         typeof(String).GetMethod("Concat",new Type[]{typeof(string),typeof(string)});
      myMethodIL.Emit(OpCodes.Call, infoMethod);
      myMethodIL.Emit(OpCodes.Ret);

      MethodInfo myHelloMethodInfo =
         typeof(IMyInterface).GetMethod("HelloMethod");
      // Implement 'HelloMethod' of 'IMyInterface'.
      myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo);
      // Create 'MyHelloWorld' type.
      Type myType = myHelloWorldType.CreateType();
      // Create 'MyNestedClass' type.
      return myNestedClassType.CreateType();
   }
}
开发者ID:.NET开发者,项目名称:System.Reflection.Emit,代码行数:68,代码来源:TypeBuilder.DefineMethod


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