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


C# MethodDefinition.Instance方法代码示例

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


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

示例1: AddGenericPrototypeCalls

        private static void AddGenericPrototypeCalls(MethodDefinition method, MethodDefinition real_method)
        {
            //external types
            Type[] systemFuncs;

            //TODO: find a better way to check for void
            if(method.ReturnType.FullName!="System.Void")
                systemFuncs = new Type[]{ typeof(Func<>), typeof(Func<,>), typeof(Func<,,>), typeof(Func<,,,>) };
            else
                systemFuncs = new Type[] { typeof(Action<>), typeof(Action<,>), typeof(Action<,,>), typeof(Action<,,,>) };

            var systemFuncClass = method.Module.Import(systemFuncs[method.GenericParameters.Count-1]);
            var systemTypeClass = method.Module.Import(typeof(Type));

            //external call
            var TypeFromHandle = method.Module.Import(systemTypeClass.Resolve().Methods.Single(m => m.Name == "GetTypeFromHandle"));

            //setup Dictionary<Type,object> and it's ContainsKey method
            var dictType = method.Module.Import(typeof(Dictionary<Type, object>));
            var dictContainsKeyMethod = method.Module.Import(dictType.Resolve().Methods.Single(m => m.Name == "ContainsKey"));
            var dictGetItemMethod = method.Module.Import(dictType.Resolve().Methods.Single(m => m.Name == "get_Item"));
            dictContainsKeyMethod.DeclaringType = dictType;
            dictGetItemMethod.DeclaringType = dictType;

            //setup prototype class and delegate for method
            var protoClassType = method.DeclaringType.NestedTypes.Single(m => m.Name == "PrototypeClass");
            var protoDelegateType = protoClassType.NestedTypes.Single(m => m.Name == "Callback_" + ComposeFullMethodName(method));

            //setup fields
            var protoField = method.DeclaringType.Fields.Single(m => m.Name == method.DeclaringType.Name.Replace('`', '_') + "Prototype");
            var staticProtoField = method.DeclaringType.Fields.Single(m => m.Name == "StaticPrototype");
            var dictProperty = protoClassType.Properties.Single(m => m.Name == ComposeFullMethodName(method));

            //setup delegate proto
            var protoDelegate = new GenericInstanceType(protoDelegateType);
            foreach (var param in method.DeclaringType.GenericParameters.Concat(method.GenericParameters))
                protoDelegate.GenericArguments.Add(param);

            //setup invoke method
            var protoInvoke = protoDelegateType.Methods.Single(m => m.Name == "Invoke");

            var il = method.Body.GetILProcessor();

            //this is a place to store instructions, which operands needs to be replaced
            List<Instruction> jmpReplacements = new List<Instruction>();

            var allParamsCount = method.Parameters.Count + (method.IsStatic ? 0 : 1);

            //setup body variables
            method.Body.InitLocals = true;
            method.Body.Variables.Add(new VariableDefinition(systemTypeClass)); //key

            if(method.ReturnType.FullName!="System.Void")
                method.Body.Variables.Add(new VariableDefinition(method.ReturnType)); //data to return

            method.Body.Variables.Add(new VariableDefinition(method.Module.Import(typeof(bool)))); //for evaluation of conditions

            il.Emit(OpCodes.Nop);

            //making key variable
            systemFuncClass = new GenericInstanceType(systemFuncClass);
            foreach (var param in method.GenericParameters)
                ((GenericInstanceType)systemFuncClass).GenericArguments.Add(param);

            il.Emit(OpCodes.Ldtoken, systemFuncClass);
            il.Emit(OpCodes.Call, TypeFromHandle);
            il.Emit(OpCodes.Stloc_0);

            if (!method.IsStatic && !method.IsConstructor)
            {
                //finding key in dictionary
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldflda, protoField.Instance());
                il.Emit(OpCodes.Call, dictProperty.GetMethod.Instance());
                il.Emit(OpCodes.Ldloc_0);
                il.Emit(OpCodes.Callvirt, dictContainsKeyMethod);
                il.Emit(OpCodes.Ldc_I4_0);
                il.Emit(OpCodes.Ceq);
                il.Emit(OpCodes.Brtrue_S, label("KeyNotFound")); //will be replaced

                //if key is found - call proto function
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldflda, protoField.Instance()); // valuetype Cheese.GenericStorage`1/Test<!0> class Cheese.GenericStorage`1<!T>::testSample
                il.Emit(OpCodes.Call, dictProperty.GetMethod.Instance());
                il.Emit(OpCodes.Ldloc_0);
                il.Emit(OpCodes.Callvirt, dictGetItemMethod); // instance !1 class [mscorlib]System.Collections.Generic.Dictionary`2<class [mscorlib]System.Type[], object>::get_Item(!0)
                il.Emit(OpCodes.Castclass, protoDelegate); // class Cheese.GenericStorage`1/Test/Maker`1<!T, !!L>
                for (int i = 0; i < allParamsCount; i++)
                    il.Emit(OpCodes.Ldarg, i);

                il.Emit(OpCodes.Callvirt, protoInvoke.Instance(method.DeclaringType.GenericParameters, method.GenericParameters)); // instance !1 class Cheese.GenericStorage`1/Test/Maker`1<!T, !!L>::Invoke(class Cheese.GenericStorage`1<!0>, !1)
                il.Emit(OpCodes.Ret);

                il.SetLabel("KeyNotFound");
            }

            //finding key in static prototype dictionary
            il.Emit(OpCodes.Ldsflda, staticProtoField.Instance());
            il.Emit(OpCodes.Call, dictProperty.GetMethod.Instance());
            il.Emit(OpCodes.Ldloc_0);
//.........这里部分代码省略.........
开发者ID:vlad-zapp,项目名称:Mice,代码行数:101,代码来源:Program.cs

示例2: AddPrototypeCalls

        private static void AddPrototypeCalls(MethodDefinition method, MethodDefinition real_method)
        {
            var staticPrototypeField = method.DeclaringType.Fields.Single(m => m.Name == "StaticPrototype");
            var dynamicPrototypeField = method.DeclaringType.Fields.SingleOrDefault(m => m.Name == method.DeclaringType.Name.Replace('`', '_') + "Prototype");
            var delegateField = method.DeclaringType.NestedTypes.Single(m => m.Name == "PrototypeClass").Fields.Single(m => m.Name == ComposeFullMethodName(method));

            var il = method.Body.GetILProcessor();

            TypeDefinition delegateType = delegateField.FieldType.Resolve();
            var invokeMethod = delegateType.Methods.Single(m => m.Name == "Invoke");
            int allParamsCount = method.Parameters.Count + (method.IsStatic ? 0 : 1); //all params and maybe this

            if (!method.IsStatic && !method.IsConstructor)
            {
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldflda, dynamicPrototypeField.Instance());
                il.Emit(OpCodes.Ldfld, delegateField.Instance());
                il.Emit(OpCodes.Brfalse, label("nextCheck"));

                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldflda, dynamicPrototypeField.Instance());
                il.Emit(OpCodes.Ldfld, delegateField.Instance());
                for (int i = 0; i < allParamsCount; i++) il.Emit(OpCodes.Ldarg, i);
                il.Emit(OpCodes.Callvirt, invokeMethod.Instance());
                il.Emit(OpCodes.Ret);
            }

            il.SetLabel("nextCheck");

            il.Emit(OpCodes.Ldsflda, staticPrototypeField.Instance());
            il.Emit(OpCodes.Ldfld, delegateField.Instance());
            il.Emit(OpCodes.Brfalse, label("callDefault")); //addres will be replaced

            il.Emit(OpCodes.Ldsflda, staticPrototypeField.Instance());
            il.Emit(OpCodes.Ldfld, delegateField.Instance());
            for (int i = 0; i < allParamsCount; i++) il.Emit(OpCodes.Ldarg, i);
            il.Emit(OpCodes.Callvirt, invokeMethod.Instance());
            il.Emit(OpCodes.Ret);

            il.SetLabel("callDefault");

            for (int i = 0; i < allParamsCount; i++) il.Emit(OpCodes.Ldarg, i);
            il.Emit(OpCodes.Call, real_method.Instance());
            il.Emit(OpCodes.Ret);
        }
开发者ID:vlad-zapp,项目名称:Mice,代码行数:45,代码来源:Program.cs


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