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


C# Cil.ILProcessor类代码示例

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


ILProcessor类属于Mono.Cecil.Cil命名空间,在下文中一共展示了ILProcessor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: HandleOfParameter

    void HandleOfParameter(Instruction instruction, ILProcessor ilProcessor)
    {
        //Info.OfMethod("AssemblyToProcess","MethodClass","InstanceMethod");

        var methodNameInstruction = instruction.Previous;
        var methodName = GetLdString(methodNameInstruction);

        var typeNameInstruction = methodNameInstruction.Previous;
        var typeName = GetLdString(typeNameInstruction);

        var assemblyNameInstruction = typeNameInstruction.Previous;
        var assemblyName = GetLdString(assemblyNameInstruction);

        var typeDefinition = GetTypeDefinition(assemblyName, typeName);

        var methodDefinition = typeDefinition.Methods.FirstOrDefault(x => x.Name == methodName);
        if (methodDefinition == null)
        {
            throw new WeavingException($"Could not find method named '{methodName}'.");
        }

        var methodReference = ModuleDefinition.ImportReference(methodDefinition);

        ilProcessor.Remove(typeNameInstruction);

        assemblyNameInstruction.OpCode = OpCodes.Ldtoken;
        assemblyNameInstruction.Operand = methodReference;

        instruction.Operand = getMethodFromHandle;

        ilProcessor.InsertAfter(instruction,Instruction.Create(OpCodes.Castclass,methodInfoType));
    }
开发者ID:Fody,项目名称:InfoOf,代码行数:32,代码来源:OfParameterHandler.cs

示例2: StoreStart

 public override void StoreStart(ILProcessor ilProcessor)
 {
     if (Previous.Type.Resolve().IsValueType)
         Previous.EmitAddress(ilProcessor);
     else
         Previous.Emit(ilProcessor);
 }
开发者ID:RainsSoft,项目名称:SharpLang,代码行数:7,代码来源:FieldMarshalledObjectEmitter.cs

示例3: GetServiceHash

        /// <summary>
        /// Emits a call that obtains the hash code for the current service instance.
        /// </summary>
        /// <param name="il">The <see cref="ILProcessor"/> that points to the method body.</param>
        /// <param name="module">The target module.</param>
        /// <param name="serviceInstance">The local variable that contains the service instance.</param>
        private void GetServiceHash(ILProcessor il, ModuleDefinition module, VariableDefinition serviceInstance)
        {
            il.Emit(OpCodes.Ldloc, serviceInstance);

            var getHashCodeMethod = module.ImportMethod<object>("GetHashCode");
            il.Emit(OpCodes.Callvirt, getHashCodeMethod);
        }
开发者ID:philiplaureano,项目名称:Hiro,代码行数:13,代码来源:ServiceInitializer.cs

示例4: RejigFirstInstruction

        /// <summary>
        /// Need to do this so we retain pointer from original first instruction to new first instruction (nop)
        /// for dbg file to point to it so VS debugger will step into weaved methods
        /// </summary>
        /// <param name="ilProcessor"></param>
        /// <param name="firstInstruction"></param>
        /// <returns></returns>
        private static Instruction RejigFirstInstruction(ILProcessor ilProcessor, Instruction firstInstruction)
        {
            /*
             From:
             opcode operand <-- pdb first line pointer

             To:
             nop <-- pdb first line pointer
             opcode operand <-- cloned second acting as first

             */
            // clone first instruction which will be used as actual
            var clonedSecond = CloneInstruction(firstInstruction);
            clonedSecond.Offset++;

            var sampleNop = ilProcessor.Create(OpCodes.Nop);

            // change actual first instruction to NOP
            firstInstruction.OpCode = sampleNop.OpCode;
            firstInstruction.Operand = sampleNop.Operand;

            // append second instruction which now is same as first one used to be at the start of this method
            // and actual first one is nop
            firstInstruction.Append(clonedSecond, ilProcessor);

            // return cloned second as new first instruction
            return clonedSecond;
        }
开发者ID:mdabbagh88,项目名称:YALF,代码行数:35,代码来源:MethodProcessor.cs

示例5: ModifyCallScope

        public void ModifyCallScope(MethodDefinition renamedMethod, MethodDefinition interceptorMethod, ILProcessor il,
            MethodInterceptionScopeType interceptionScope)
        {
            if (interceptionScope == MethodInterceptionScopeType.Deep)
            {
                foreach (var module in Type.Assembly.Definition.Modules)
                {
                    foreach (var type in module.Types.ToList())
                    {
                        if (type.Methods == null || type.Methods.Count == 0) continue;
                        foreach (var method in type.Methods.ToList())
                        {
                            if (Context.Marker.HasMarker(method, Method.MethodMarker)) continue;

                            if (method == null
                                || method.Body == null
                                || method.Body.Instructions == null
                                || method.Body.Instructions.Count() == 0)
                                continue;

                            foreach (var instruction in method.Body.Instructions.ToList())
                            {
                                if (instruction.OpCode == OpCodes.Call && instruction.Operand == renamedMethod)
                                {
                                    var processor = method.Body.GetILProcessor();
                                    processor.InsertAfter(instruction, il.Create(OpCodes.Call, interceptorMethod));
                                    processor.Remove(instruction);
                                }
                            }
                        }
                    }
                }
            }
        }
开发者ID:fir3pho3nixx,项目名称:cryo-aop,代码行数:34,代码来源:MethodScopingExtension.cs

示例6: Generate

        public override void Generate(ILProcessor processor)
        {
            // Load the arguments.
            foreach (VariableDefinition v in m_Parameters)
            {
                processor.Append(Instruction.Create(OpCodes.Ldloc, v));
            }

            // Call the delegate.
            processor.Append(Instruction.Create(OpCodes.Call, this.m_Target));

            // Handle the return type.
            if (this.m_ReturnType.FullName == this.m_Target.Module.Import(typeof(void)).FullName)
            {
                // Return value is void.  Discard any result and return.
                processor.Append(Instruction.Create(OpCodes.Pop));
            }
            else if (this.m_ReturnType.IsValueType || this.m_ReturnType.IsGenericParameter)
            {
                // Return value is value type (not reference).  Unbox and return it.
                processor.Append(Instruction.Create(OpCodes.Unbox_Any, this.m_ReturnType));
                processor.Append(Instruction.Create(OpCodes.Stloc, this.Result));
            }
            else
            {
                // Return value is reference type.  Cast it and return it.
                processor.Append(Instruction.Create(OpCodes.Isinst, this.m_ReturnType));
                processor.Append(Instruction.Create(OpCodes.Stloc, this.Result));
            }
        }
开发者ID:hach-que,项目名称:Dx,代码行数:30,代码来源:CallStatement.cs

示例7: EmitCodeInit

 private Instruction EmitCodeInit(TypeReference role, Instruction instructionBeforeInit, ILProcessor il)
 {
     var current = instructionBeforeInit;
       current = InsertAfter(il, current, il.Create(OpCodes.Ldarg_0));
       current = InsertAfter(il, current, il.Create(OpCodes.Call, ResolveInitReference(role)));
       return current;
 }
开发者ID:cessationoftime,项目名称:nroles,代码行数:7,代码来源:RoleComposer.Initialization.cs

示例8: CompileCil

        private void CompileCil(ILProcessor body, IAstElement element, CilCompilationContext context)
        {
            var compiler = this.cilCompilers.SingleOrDefault(c => c.CanCompile(body, element));
            if (compiler == null)
                throw new NotImplementedException("LightCompiler: No CilCompiler for " + element);

            compiler.Compile(body, element, context);
        }
开发者ID:ashmind,项目名称:light,代码行数:8,代码来源:LightCompiler.cs

示例9: EmitAddress

 public override void EmitAddress(ILProcessor ilProcessor)
 {
     if (Previous.Type.Resolve().IsValueType)
         Previous.EmitAddress(ilProcessor);
     else
         Previous.Emit(ilProcessor);
     ilProcessor.Emit(OpCodes.Ldflda, Field);
 }
开发者ID:RainsSoft,项目名称:SharpLang,代码行数:8,代码来源:FieldMarshalledObjectEmitter.cs

示例10: AssemblyResolveUpdater

 public AssemblyResolveUpdater(ModuleDefinition module)
 {
     _module = module;
     var type = _module.GetType("TheIndex", "Resolver");
     _initMethod = type.Methods.Single(m => m.Name == "DictionaryInitialization");
     _addResolveMethod = type.Methods.Single(m => m.Name == "Add");
     _proc = _initMethod.Body.GetILProcessor();
 }
开发者ID:flq,项目名称:Tmp.ExecIndex,代码行数:8,代码来源:AssemblyResolveUpdater.cs

示例11: InjectWriteIl

 static void InjectWriteIl(List<Instruction> writeTimeIl, ILProcessor ilProcessor, Instruction beforeThis)
 {
     foreach (var instruction in writeTimeIl)
     {
         ilProcessor.InsertBefore(beforeThis, instruction);
     }
     ilProcessor.InsertBefore(beforeThis, Instruction.Create(OpCodes.Endfinally));
 }
开发者ID:johnsimons,项目名称:MethodTimer,代码行数:8,代码来源:MethodProcessor.cs

示例12: IsPredicated

 public Boolean IsPredicated(Instruction instruction, ILProcessor ilProcessor) {
     try {
         return predicate(instruction, ilProcessor);
     }
     catch (Exception) {
         return false;
     }
 }
开发者ID:Kleptine,项目名称:NameOf,代码行数:8,代码来源:PatternInstruction.cs

示例13: InsertAfter

        public Instruction InsertAfter(Instruction instruction, ILProcessor processor)
        {
            var currentInstruction = instruction;
            foreach (var newInstructionBlock in InstructionBlocks)
                currentInstruction = newInstructionBlock.InsertAfter(currentInstruction, processor);

            return currentInstruction;
        }
开发者ID:swestner,项目名称:MethodBoundaryAspect.Fody,代码行数:8,代码来源:InstructionBlockChain.cs

示例14: MethodPatcher

        public MethodPatcher(PatcherObject prnt, MethodDefinition metDef) : base(prnt)
        {
            methodDefinition = metDef;
            IlProc = metDef.Body.GetILProcessor();
            rootAssemblyPatcher = prnt.rootAssemblyPatcher;

            if (MainClass.gendiffs && MainClass.newAssCS)
                original = metDef.Print();
        }
开发者ID:Notulp,项目名称:Pluton,代码行数:9,代码来源:MethodPatcher.cs

示例15: InterceptMethod

 private void InterceptMethod(ILProcessor processor, TypeReference typeReference, Instruction instruction, string name)
 {
     var typeDefinition = typeReference.Resolve();
     var attributeConstructor = typeDefinition.Methods.First(x => x.Name == ".ctor");
     var attributeMethod = typeDefinition.Methods.First(x => x.Name == name);
     processor.InsertBefore(instruction, processor.Create(OpCodes.Newobj, attributeConstructor));
     processor.InsertBefore(instruction, processor.Create(OpCodes.Call, _getCurrentMethod));
     processor.InsertBefore(instruction, processor.Create(OpCodes.Call, attributeMethod));
 }
开发者ID:AlbertoMonteiro,项目名称:MethodInterceptorAopWithFody,代码行数:9,代码来源:ModuleWeaver.cs


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