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


C# Instruction.FindStringInstruction方法代码示例

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


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

示例1: ProcessInstruction

    void ProcessInstruction(Instruction instruction)
    {
        var methodReference = instruction.Operand as MethodReference;
        if (methodReference == null)
        {
            return;
        }
        if (methodReference.DeclaringType.FullName != "Anotar.CommonLogging.LogTo")
        {
            return;
        }
        if (!foundUsageInMethod)
        {
            Method.Body.InitLocals = true;
            Method.Body.SimplifyMacros();
        }
        foundUsageInMethod = true;
        FoundUsageInType();


        var parameters = methodReference.Parameters;
        var instructions = Method.Body.Instructions;
        instruction.OpCode = OpCodes.Callvirt;

        if (parameters.Count == 0)
        {
            instructions.Replace(instruction, new[]
                                              {
                                                  Instruction.Create(OpCodes.Ldsfld, Field),
                                                  Instruction.Create(OpCodes.Ldstr, GetMessagePrefix(instruction)),
                                                  Instruction.Create(OpCodes.Ldc_I4_0),
                                                  Instruction.Create(OpCodes.Newarr, ModuleWeaver.ModuleDefinition.TypeSystem.Object),
                                                  Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetNormalOperand(methodReference))
                                              });
            return;
        }
        if (methodReference.IsMatch("String", "Exception", "Object[]"))
        {
            if (messageVar == null)
            {
                messageVar = new VariableDefinition(ModuleWeaver.ModuleDefinition.TypeSystem.String);
                Method.Body.Variables.Add(messageVar);
            }
            if (exceptionVar == null)
            {
                exceptionVar = new VariableDefinition(ModuleWeaver.ExceptionType);
                Method.Body.Variables.Add(exceptionVar);
            }
            if (paramsVar == null)
            {
                paramsVar = new VariableDefinition(ModuleWeaver.ObjectArray);
                Method.Body.Variables.Add(paramsVar);
            }
            instructions.Replace(instruction, new[]
                                              {
                                                  Instruction.Create(OpCodes.Stloc, paramsVar),
                                                  Instruction.Create(OpCodes.Stloc, exceptionVar),
                                                  Instruction.Create(OpCodes.Stloc, messageVar),
                                                  Instruction.Create(OpCodes.Ldsfld, Field),
                                                  Instruction.Create(OpCodes.Ldstr, GetMessagePrefix(instruction)),
                                                  Instruction.Create(OpCodes.Ldloc, messageVar),
                                                  Instruction.Create(OpCodes.Call, ModuleWeaver.ConcatMethod),
                                                  Instruction.Create(OpCodes.Ldloc, exceptionVar),
                                                  Instruction.Create(OpCodes.Ldloc, paramsVar),
                                                  Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetExceptionOperand(methodReference)),
                                              });
            return;
        }
        if (methodReference.IsMatch("String", "Object[]"))
        {
            var stringInstruction =instruction.FindStringInstruction();

            if (stringInstruction != null)
            {
                var operand = GetMessagePrefix(instruction) + (string) stringInstruction.Operand;
                instructions.Replace(stringInstruction, new[]
                                                        {
                                                            Instruction.Create(OpCodes.Ldsfld, Field),
                                                            Instruction.Create(stringInstruction.OpCode, operand),
                                                        });

                instruction.Operand = ModuleWeaver.GetNormalOperand(methodReference);
                return;
            }
            if (messageVar == null)
            {
                messageVar = new VariableDefinition(ModuleWeaver.ModuleDefinition.TypeSystem.String);
                Method.Body.Variables.Add(messageVar);
            }
            if (paramsVar == null)
            {
                paramsVar = new VariableDefinition(ModuleWeaver.ObjectArray);
                Method.Body.Variables.Add(paramsVar);
            }


            instructions.Replace(instruction, new[]
                                              {
                                                  Instruction.Create(OpCodes.Stloc, paramsVar),
                                                  Instruction.Create(OpCodes.Stloc, messageVar),
//.........这里部分代码省略.........
开发者ID:vlaci,项目名称:Anotar,代码行数:101,代码来源:LogForwardingProcessor.cs

示例2: HandleStringAndArray

    void HandleStringAndArray(Instruction instruction, MethodReference methodReference)
    {
        var instructions = Method.Body.Instructions;
        var stringInstruction = instruction.FindStringInstruction();

        if (stringInstruction == null)
        {
            if (messageVar == null)
            {
                messageVar = new VariableDefinition(ModuleWeaver.ModuleDefinition.TypeSystem.String);
                Method.Body.Variables.Add(messageVar);
            }
            if (paramsVar == null)
            {
                paramsVar = new VariableDefinition(ModuleWeaver.ObjectArray);
                Method.Body.Variables.Add(paramsVar);
            }

            var exitNop = Instruction.Create(OpCodes.Nop);

            var replacement = new List<Instruction>
                              {
                                  // store the variables
                                  Instruction.Create(OpCodes.Stloc, paramsVar),
                                  Instruction.Create(OpCodes.Stloc, messageVar),

                                  //Append if 
                                  Instruction.Create(OpCodes.Ldsfld, LoggerField),
                                  Instruction.Create(OpCodes.Ldc_I4, ModuleWeaver.GetLevelForMethodName(methodReference)),
                                  Instruction.Create(OpCodes.Callvirt, ModuleWeaver.IsEnabledMethod),
                                  Instruction.Create(OpCodes.Brfalse, exitNop)
                              };

            AppendExtraContext(instruction, replacement);
            replacement.Append(
                //put the variable back on the stack params
                Instruction.Create(OpCodes.Ldloc, messageVar),
                Instruction.Create(OpCodes.Ldloc, paramsVar),
                //call the write method
                Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetNormalOperand(methodReference)),
                exitNop
                );
            ;
            instructions.Replace(instruction, replacement);
        }
        else
        {
            var exitNop = Instruction.Create(OpCodes.Nop);
            var replacement = new List<Instruction>
                              {

                                  Instruction.Create(OpCodes.Ldsfld, LoggerField),
                                  Instruction.Create(OpCodes.Ldc_I4, ModuleWeaver.GetLevelForMethodName(methodReference)),
                                  Instruction.Create(OpCodes.Callvirt, ModuleWeaver.IsEnabledMethod),
                                  Instruction.Create(OpCodes.Brfalse, exitNop)
                              };

            AppendExtraContext(instruction, replacement);
            replacement.Append(
                //re-write stringInstruction contentes 
                Instruction.Create(stringInstruction.OpCode, (string) stringInstruction.Operand)
                );
            instructions.Replace(stringInstruction, replacement);

            instruction.Operand = ModuleWeaver.GetNormalOperand(methodReference);
            
            instructions.Insert(instructions.IndexOf(instruction)+1, exitNop);
        }
    }
开发者ID:modulexcite,项目名称:Anotar,代码行数:69,代码来源:LogForwardingProcessor.cs

示例3: ProcessInstruction


//.........这里部分代码省略.........
            {
                funcVar = new VariableDefinition(ModuleWeaver.GenericFunc);
                Method.Body.Variables.Add(funcVar);
            }
            if (exceptionVar == null)
            {
                exceptionVar = new VariableDefinition(ModuleWeaver.ExceptionType);
                Method.Body.Variables.Add(exceptionVar);
            }

            var sectionNop = Instruction.Create(OpCodes.Nop);
            instructions.Replace(instruction,
                new[]
                {
                    Instruction.Create(OpCodes.Stloc, funcVar),
                    Instruction.Create(OpCodes.Stloc, exceptionVar),
                    Instruction.Create(OpCodes.Ldsfld, LoggerField),
                    Instruction.Create(OpCodes.Callvirt, isEnabledMethod),
                    Instruction.Create(OpCodes.Brfalse_S, sectionNop),
                    Instruction.Create(OpCodes.Ldsfld, LoggerField),
                    Instruction.Create(OpCodes.Ldloc, exceptionVar),
                    Instruction.Create(OpCodes.Ldstr, messagePrefix),
                    Instruction.Create(OpCodes.Ldloc, funcVar),
                    Instruction.Create(OpCodes.Callvirt, ModuleWeaver.FuncInvokeMethod),
                    Instruction.Create(OpCodes.Call, ModuleWeaver.ConcatMethod),
                    Instruction.Create(OpCodes.Ldnull),
                    Instruction.Create(OpCodes.Callvirt, ModuleWeaver.GetExceptionOperand(methodReference)),
                    sectionNop
                });
            return;
        }
        if (methodReference.IsMatch("String", "Object[]"))
        {
            var stringInstruction = instruction.FindStringInstruction();

            if (stringInstruction != null)
            {

                var operand = messagePrefix + (string) stringInstruction.Operand;
                instructions.Replace(stringInstruction,
                    new[]
                    {
                        Instruction.Create(OpCodes.Ldsfld, LoggerField),
                        Instruction.Create(stringInstruction.OpCode, operand),
                    });

                instruction.Operand = ModuleWeaver.GetNormalOperand(methodReference);
                return;
            }
            if (messageVar == null)
            {
                messageVar = new VariableDefinition(ModuleWeaver.ModuleDefinition.TypeSystem.String);
                Method.Body.Variables.Add(messageVar);
            }
            if (paramsVar == null)
            {
                paramsVar = new VariableDefinition(ModuleWeaver.ObjectArray);
                Method.Body.Variables.Add(paramsVar);
            }

            instructions.Replace(instruction,
                new[]
                {
                    Instruction.Create(OpCodes.Stloc, paramsVar),
                    Instruction.Create(OpCodes.Stloc, messageVar),
                    Instruction.Create(OpCodes.Ldsfld, LoggerField),
开发者ID:modulexcite,项目名称:Anotar,代码行数:67,代码来源:LogForwardingProcessor.cs


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