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


C# Instruction.ChangeToNop方法代码示例

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


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

示例1: InlineCall


//.........这里部分代码省略.........
                            break;
                        case Code.Ldarga:
                            {
                                var index = targetMethod.Parameters.IndexOf((ParameterDefinition)instr.Operand);
                                ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                                ni.OpCode = OpCodes.Ldloca;
                            }
                            break;
                        case Code.Starg:
                            {
                                var index = targetMethod.Parameters.IndexOf((ParameterDefinition)instr.Operand);
                                ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                                ni.OpCode = OpCodes.Stloc;
                            }
                            break;
                    }
                }

                // Update branch targets
                for (var i = 0; i < seq.Length; i++)
                {
                    var instr = seq[i];
                    var oldi = source.Instructions[i];

                    if (instr.OpCode.OperandType == OperandType.InlineSwitch)
                    {
                        var olds = (Instruction[])oldi.Operand;
                        var targets = new Instruction[olds.Length];

                        for (int j = 0; j < targets.Length; j++)
                        {
                            targets[j] = GetClone(seq, source.Instructions, olds[j]);
                        }

                        instr.Operand = targets;
                    }
                    else if (instr.OpCode.OperandType == OperandType.ShortInlineBrTarget ||
                             instr.OpCode.OperandType == OperandType.InlineBrTarget)
                    {
                        instr.Operand = GetClone(seq, source.Instructions, (Instruction)oldi.Operand);
                    }
                }

                // Clone exception handlers
                if (source.HasExceptionHandlers)
                {
                    body.ComputeOffsets();
                    CloneInstructions(seq, source.Instructions, body.ExceptionHandlers, source.ExceptionHandlers);
                }

                // Replace ret instructions
                var end = seq.Emit(OpCodes.Nop);
                var retInstructions = seq.Where(x => x.OpCode.Code == Code.Ret).ToList();
                foreach (var ins in retInstructions)
                {
                    ins.OpCode = OpCodes.Br;
                    ins.Operand = end;
                }

                // cast return type of a generic method. TODO: better might be to
                // correctly resolve calls to generic instances as well (above)
                if (targetMethod.ReturnType.IsGenericParameter)
                {
                    var methodRef = (MethodReference)instruction.Operand;
                    TypeReference returnType = null;

                    var gp = (GenericParameter)methodRef.ReturnType;
                    if (gp.Type == GenericParameterType.Type)
                    {
                        var gi = methodRef.DeclaringType as IGenericInstance;
                        if (gi != null && gi.HasGenericArguments)
                            returnType = gi.GenericArguments[gp.Position];
                    }
                    else if (gp.Type == GenericParameterType.Method)
                    {
                        var gi = methodRef as IGenericInstance;
                        if (gi != null && gi.HasGenericArguments)
                            returnType = gi.GenericArguments[gp.Position];
                    }

                    if (returnType != null)
                    {
                        if (!returnType.IsPrimitive)
                        {
                            seq.Emit(OpCodes.Castclass, returnType);
                        }
                        // todo: handle primitive types. unbox them? are structs correctly handled? enums?
                    }
                }

                // Insert cloned instructions
                prefixSeq.InsertTo(0, seq);
                seq.InsertToAfter(instruction, body);

                // Change replaced instruction to nop
                instruction.ChangeToNop();

                // Update offsets
                body.ComputeOffsets();
            }
开发者ID:Xtremrules,项目名称:dot42,代码行数:101,代码来源:InlineMethods.cs

示例2: InlineNewObjCall


//.........这里部分代码省略.........
                            }
                            break;
                    }

                    // Convert parameter opcodes
                    switch (instr.OpCode.Code)
                    {
                        case Mono.Cecil.Cil.Code.Ldarg:
                            {
                                var index = ctor.Parameters.IndexOf((ParameterDefinition) instr.Operand);
                                ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                                ni.OpCode = OpCodes.Ldloc;
                            }
                            break;
                        case Mono.Cecil.Cil.Code.Ldarga:
                            {
                                var index = ctor.Parameters.IndexOf((ParameterDefinition) instr.Operand);
                                ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                                ni.OpCode = OpCodes.Ldloca;
                            }
                            break;
                        case Mono.Cecil.Cil.Code.Starg:
                            {
                                var index = ctor.Parameters.IndexOf((ParameterDefinition) instr.Operand);
                                ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                                ni.OpCode = OpCodes.Stloc;
                            }
                            break;
                    }
                }

                // Update branch targets
                for (var i = 0; i < seq.Length; i++)
                {
                    var instr = seq[i];
                    var oldi = source.Instructions[i];

                    if (instr.OpCode.OperandType == OperandType.InlineSwitch)
                    {
                        var olds = (Instruction[]) oldi.Operand;
                        var targets = new Instruction[olds.Length];

                        for (int j = 0; j < targets.Length; j++)
                        {
                            targets[j] = GetClone(seq, source.Instructions, olds[j]);
                        }

                        instr.Operand = targets;
                    }
                    else if (instr.OpCode.OperandType == OperandType.ShortInlineBrTarget ||
                             instr.OpCode.OperandType == OperandType.InlineBrTarget)
                    {
                        instr.Operand = GetClone(seq, source.Instructions, (Instruction) oldi.Operand);
                    }
                }

                // Clone exception handlers
                if (source.HasExceptionHandlers)
                {
                    CloneInstructions(seq, source.Instructions, body.ExceptionHandlers, source.ExceptionHandlers);
                }

                // Find call to "this" ctor
                var callToCtors = seq.Where(x => IsCallToThisCtor(x, ctor)).ToList();
                if (callToCtors.Count == 0)
                    throw new CompilerException(string.Format("No call to another this ctor found in {0}", ctor));
                if (callToCtors.Count > 1)
                    throw new CompilerException(string.Format("Multiple calls to another this ctor found in {0}", ctor));
                var callToCtor = callToCtors[0];

                // Change "ld this" to nop
                var args = callToCtor.GetCallArguments(seq, true);
                args[0].ChangeToNop(); // Replace ldarg.0

                // Replace call to this ctor with newobj
                var callSeq = new ILSequence();
                callSeq.Emit(OpCodes.Newobj, (MethodReference) callToCtor.Operand);
                callSeq.Emit(OpCodes.Stloc, thisVariable); // Save new object
                callToCtor.ChangeToNop();
                callSeq.InsertToBefore(callToCtor, seq);

                // Replace ret instructions
                var end = seq.Emit(OpCodes.Ldloc, thisVariable);
                var retInstructions = seq.Where(x => x.OpCode.Code == Mono.Cecil.Cil.Code.Ret).ToList();
                foreach (var ins in retInstructions)
                {
                    ins.OpCode = OpCodes.Br;
                    ins.Operand = end;
                }

                // Insert cloned instructions
                prefixSeq.InsertTo(0, seq);
                seq.InsertToAfter(instruction, body);

                // Change replaced instruction to nop
                instruction.ChangeToNop();

                // Update offsets
                body.ComputeOffsets();
            }
开发者ID:Xtremrules,项目名称:dot42,代码行数:101,代码来源:InlineAndroidExtensionCtors.cs

示例3: InlineCall


//.........这里部分代码省略.........
                }

                // Now clone instructions
                var seq = new ILSequence();
                foreach (var instr in source.Instructions)
                {
                    var ni = new Instruction(instr.OpCode, instr.Operand);
                    seq.Append(ni);
                    ni.Offset = instr.Offset;

                    // Convert variable opcodes
                    switch (instr.OpCode.OperandType)
                    {
                        case OperandType.InlineVar:
                        case OperandType.ShortInlineVar:
                            {
                                var index = source.Variables.IndexOf((VariableDefinition)instr.Operand);
                                ni.Operand = variables[index];
                            }
                            break;
                    }

                    // Convert parameter opcodes
                    switch (instr.OpCode.Code)
                    {
                        case Code.Ldarg:
                            {
                                var index = targetMethod.Parameters.IndexOf((ParameterDefinition)instr.Operand);
                                ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                                ni.OpCode = OpCodes.Ldloc;
                            }
                            break;
                        case Code.Ldarga:
                            {
                                var index = targetMethod.Parameters.IndexOf((ParameterDefinition)instr.Operand);
                                ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                                ni.OpCode = OpCodes.Ldloca;
                            }
                            break;
                        case Code.Starg:
                            {
                                var index = targetMethod.Parameters.IndexOf((ParameterDefinition)instr.Operand);
                                ni.Operand = (index >= 0) ? paramVariables[index] : thisVariable;
                                ni.OpCode = OpCodes.Stloc;
                            }
                            break;
                    }
                }

                // Update branch targets
                for (var i = 0; i < seq.Length; i++)
                {
                    var instr = seq[i];
                    var oldi = source.Instructions[i];

                    if (instr.OpCode.OperandType == OperandType.InlineSwitch)
                    {
                        var olds = (Instruction[])oldi.Operand;
                        var targets = new Instruction[olds.Length];

                        for (int j = 0; j < targets.Length; j++)
                        {
                            targets[j] = GetClone(seq, source.Instructions, olds[j]);
                        }

                        instr.Operand = targets;
                    }
                    else if (instr.OpCode.OperandType == OperandType.ShortInlineBrTarget ||
                             instr.OpCode.OperandType == OperandType.InlineBrTarget)
                    {
                        instr.Operand = GetClone(seq, source.Instructions, (Instruction)oldi.Operand);
                    }
                }

                // Clone exception handlers
                if (source.HasExceptionHandlers)
                {
                    body.ComputeOffsets();
                    CloneInstructions(seq, source.Instructions, body.ExceptionHandlers, source.ExceptionHandlers);
                }

                // Replace ret instructions
                var end = seq.Emit(OpCodes.Nop);
                var retInstructions = seq.Where(x => x.OpCode.Code == Code.Ret).ToList();
                foreach (var ins in retInstructions)
                {
                    ins.OpCode = OpCodes.Br;
                    ins.Operand = end;
                }

                // Insert cloned instructions
                prefixSeq.InsertTo(0, seq);
                seq.InsertToAfter(instruction, body);

                // Change replaced instruction to nop
                instruction.ChangeToNop();

                // Update offsets
                body.ComputeOffsets();
            }
开发者ID:rfcclub,项目名称:dot42,代码行数:101,代码来源:InlineMethods.cs


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