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


C# Instruction.Prepend方法代码示例

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


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

示例1: InjectIlForCatch

        private Instruction InjectIlForCatch(ILProcessor processor, Instruction beforeReturn, ReferenceContainer references)
        {
            /*
                stloc.2

                nop
                ldloc.2
                call void [Yalf]Yalf.Log::TraceException(class [mscorlib]System.Exception)

                nop
                ldloc.0
                ldloc.2
                callvirt   instance void [Yalf]Yalf.IContext::PreserveStackTrace(class [mscorlib]System.Exception)

                nop
                rethrow
             */

            var exceptionVariable = new VariableDefinition(references.ExceptionType);
            body.Variables.Add(exceptionVariable);

            var beginCatch = beforeReturn.Prepend(processor.Create(OpCodes.Stloc, exceptionVariable), processor);
            beginCatch.Append(
                           processor.Create(OpCodes.Nop), processor)
                          .AppendLdloc(processor, exceptionVariable.Index)
                          .Append(processor.Create(OpCodes.Call, references.TraceExceptionMethod), processor)

                          .Append(processor.Create(OpCodes.Nop), processor)
                          .AppendLdloc(processor, contextVar.Index)
                          .AppendLdloc(processor, exceptionVariable.Index)
                          .Append(processor.Create(OpCodes.Callvirt, references.PreserveStackTraceMethod), processor)

                          .Append(processor.Create(OpCodes.Nop), processor)
                          .Append(processor.Create(OpCodes.Rethrow), processor);

            return beginCatch;
        }
开发者ID:mdabbagh88,项目名称:YALF,代码行数:37,代码来源:MethodProcessor.cs

示例2: InjectIlForFinaly

        private Instruction InjectIlForFinaly(ILProcessor processor, Instruction beforeReturn, ReferenceContainer references)
        {
            // wrapped in nop (for try catch handling)
            // load context then call dispose
            /*
                nop
                ldloc.0
                callvirt instance void [Yalf]Yalf.IContext::Dispose()
                nop
                endfinally
             */

            var beginFinally = beforeReturn.Prepend(processor.Create(OpCodes.Nop), processor);

            beginFinally.AppendLdloc(processor, contextVar.Index)
                .Append(processor.Create(OpCodes.Callvirt, references.DisposeMethod), processor)
                .Append(processor.Create(OpCodes.Nop), processor)
                .Append(processor.Create(OpCodes.Endfinally), processor);

            return beginFinally;
        }
开发者ID:mdabbagh88,项目名称:YALF,代码行数:21,代码来源:MethodProcessor.cs

示例3: InjectContext

        void InjectContext(ILProcessor processor, Instruction firstInstruction, MethodDefinition method, ReferenceContainer references)
        {
            /*
            nop
            ldstr "Namespace.Class.Method"
            ldc.i4.1
            newarr object
            stloc.s CS$0$0001
            ldloc.s CS$0$0001
            ldc.i4.0
            ldarg.1
            box int32
            stelem.ref
            ldloc.s CS$0$0001
            call class [Yalf]Yalf.IContext [Yalf]Yalf.Log::MethodContext(string, object[])
            stloc.0
            */

            contextVar = new VariableDefinition(references.IContextType);
            body.Variables.Insert(0, contextVar);

            int objectParamsArrayIndex = method.AddVariable<object[]>();

            // Generate MethodContext calling method name
            var builder = new StringBuilder();

            builder.Append(string.Join(".",
                method.DeclaringType.Namespace,
                FormatType(method.DeclaringType),
                FormatMethod(method)));

            var current = firstInstruction
                .Prepend(processor.Create(OpCodes.Ldstr, builder.ToString()), processor);

            // Create object[] for MethodContext
            current = current
                .AppendLdcI4(processor, method.Parameters.Count)
                .Append(processor.Create(OpCodes.Newarr, method.Module.ImportType<object>()), processor)
                .AppendStloc(processor, objectParamsArrayIndex);

            var nonStaticMethodAddOne = method.IsStatic ? 0 : 1;

            // Set object[] values
            for (int i = 0; i < method.Parameters.Count; i++)
            {
                current = current
                    .AppendLdloc(processor, objectParamsArrayIndex)
                    .AppendLdcI4(processor, i);

                var paramType = method.Parameters[i].ParameterType;

                if (paramType.MetadataType == MetadataType.UIntPtr ||
                    paramType.MetadataType == MetadataType.FunctionPointer ||
                    paramType.MetadataType == MetadataType.IntPtr ||
                    paramType.MetadataType == MetadataType.Pointer)
                {
                    // don't store pointer types into object[] (we can't ToString them anyway)
                    // store type name as string instead
                    current = current.AppendLdstr(processor, paramType.FullName);
                }
                else
                {
                    current = current
                        .AppendLdarg(processor, i + nonStaticMethodAddOne)
                        .AppendBoxAndResolveRefIfNecessary(processor, paramType);
                }

                current = current.Append(processor.Create(OpCodes.Stelem_Ref), processor);
            }

            // Call Log.MethodContext
            current
                .AppendLdloc(processor, objectParamsArrayIndex)
                .Append(processor.Create(OpCodes.Call, references.MethodContextMethod), processor)
                .AppendStloc(processor, contextVar.Index);
        }
开发者ID:mdabbagh88,项目名称:YALF,代码行数:76,代码来源:MethodProcessor.cs


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