• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# ILGenerator.DefineLabel方法代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: BuildAdderType

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

class ILLabelDemo {

   public static Type BuildAdderType() {

    AppDomain myDomain = Thread.GetDomain();
    AssemblyName myAsmName = new AssemblyName();
    myAsmName.Name = "AdderExceptionAsm";
    AssemblyBuilder myAsmBldr = myDomain.DefineDynamicAssembly(myAsmName,
                                 AssemblyBuilderAccess.Run);

    ModuleBuilder myModBldr = myAsmBldr.DefineDynamicModule("AdderExceptionMod");

    TypeBuilder myTypeBldr = myModBldr.DefineType("Adder");

    Type[] adderParams = new Type[] {typeof(int), typeof(int)};

    // This method will add two numbers which are 100 or less. If either of the
    // passed integer vales are greater than 100, it will return the value of -1.

    MethodBuilder adderBldr = myTypeBldr.DefineMethod("DoAdd",
                            MethodAttributes.Public |
                            MethodAttributes.Static,
                            typeof(int),
                            adderParams);
    ILGenerator adderIL = adderBldr.GetILGenerator();

    // In order to successfully branch, we need to create labels
    // representing the offset IL instruction block to branch to.
    // These labels, when the MarkLabel(Label) method is invoked,
    // will specify the IL instruction to branch to.
                                                                
    Label failed = adderIL.DefineLabel();
    Label endOfMthd = adderIL.DefineLabel();

    // First, load argument 0 and the integer value of "100" onto the
    // stack. If arg0 > 100, branch to the label "failed", which is marked
    // as the address of the block that loads -1 onto the stack, bypassing
    // the addition.

    adderIL.Emit(OpCodes.Ldarg_0);
    adderIL.Emit(OpCodes.Ldc_I4_S, 100);
    adderIL.Emit(OpCodes.Bgt_S, failed); 

    // Now, check to see if argument 1 was greater than 100. If it was,
    // branch to "failed." Otherwise, fall through and perform the addition,
    // branching unconditionally to the instruction at the label "endOfMthd".

    adderIL.Emit(OpCodes.Ldarg_1);
    adderIL.Emit(OpCodes.Ldc_I4_S, 100);
    adderIL.Emit(OpCodes.Bgt_S, failed);

    adderIL.Emit(OpCodes.Ldarg_0);
    adderIL.Emit(OpCodes.Ldarg_1);
    adderIL.Emit(OpCodes.Add_Ovf_Un);
    adderIL.Emit(OpCodes.Br_S, endOfMthd);

    // If this label is branched to (the failure case), load -1 onto the stack
    // and fall through to the return opcode.
    adderIL.MarkLabel(failed);
    adderIL.Emit(OpCodes.Ldc_I4_M1);

    // The end of the method. If both values were less than 100, the
    // correct result will return. If one of the arguments was greater
    // than 100, the result will be -1. 

    adderIL.MarkLabel(endOfMthd);
    adderIL.Emit(OpCodes.Ret);
    
    return myTypeBldr.CreateType();
   }

   public static void Main() {

    Type adderType = BuildAdderType();

    object addIns = Activator.CreateInstance(adderType); 

    object[] addParams = new object[2];

    Console.Write("Enter an integer value: ");
    addParams[0] = (object)Convert.ToInt32(Console.ReadLine());

    Console.Write("Enter another integer value: ");
    addParams[1] = (object)Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("---");

    int adderResult = (int)adderType.InvokeMember("DoAdd",
                    BindingFlags.InvokeMethod,
                    null,
                    addIns,
                    addParams); 

    if (adderResult != -1) {

        Console.WriteLine("{0} + {1} = {2}", addParams[0], addParams[1],
                  adderResult);
    } else {

        Console.WriteLine("One of the integers to add was greater than 100!");
    }		
   }
}
开发者ID:.NET开发者,项目名称:System.Reflection.Emit,代码行数:109,代码来源:ILGenerator.DefineLabel



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# ILGenerator.Emit方法代码示例发布时间:2022-05-26
下一篇:
C# ILGenerator.DeclareLocal方法代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap