예제 #1
0
        public override void DoInjectProperty(AssemblyDefinition assembly, PropertyDefinition property, TypeDefinition type)
        {
            var        methods       = typeof(EventDispatcher).GetMethods();
            MethodInfo triggerMethod = null;

            foreach (var method in methods)
            {
                if (method.Name == "TriggerEvent" && method.GetGenericArguments().Count() == 1)
                {
                    triggerMethod = method;
                    break;
                }
            }

            var triggerRef = assembly.MainModule.Import(triggerMethod);

            //var objectRef = assembly.MainModule.Import(typeof(object));
            triggerRef = triggerRef.MakeGeneric(property.PropertyType);
            var typeRef  = assembly.MainModule.Import(typeof(int));
            var fieldRef = type.Fields.Single(field => field.Name == GetHiddenFieldName(property));

            InjectEmitHelper.InsertBefore(property.SetMethod.GetILProcessor(), property.SetMethod.GetFirstInstriction(),
                                          new Dictionary <OpCode, object>()
            {
                { OpCodes.Ldstr, type.Name + "." + property.Name },
                { OpCodes.Ldarg_1, null },
                //{ OpCodes.Castclass, objectRef},
                { OpCodes.Ldnull, null },
                { OpCodes.Call, triggerRef },
            });



            InjectEmitHelper.ComputeOffsets(property.SetMethod.Body);
        }
예제 #2
0
        /// <summary>
        /// 插入输出语句
        /// </summary>
        /// <param name="assembly"></param>
        /// <param name="worker"></param>
        /// <param name="target"></param>
        /// <param name="text"></param>
        /// <returns></returns>
        public static Instruction InsertLogBefore(AssemblyDefinition assembly, ILProcessor worker, Instruction target, string text)
        {
            if (logMethod == null)
            {
                logMethod = assembly.MainModule.Import(typeof(Debug).GetMethod("Log", new Type[] { typeof(string) }));
            }
            var current = InjectEmitHelper.InsertBefore(worker, target, worker.Create(OpCodes.Ldstr, "Inject"));

            current = InjectEmitHelper.InsertBefore(worker, target, worker.Create(OpCodes.Call, logMethod));
            return(current);
        }
예제 #3
0
        public override void DoInjectMethod(AssemblyDefinition assembly, MethodDefinition method, TypeDefinition type)
        {
            if (!InjectEmitHelper.HasBodyAndIsNotContructor(method) || method.IsGetter || method.IsSetter) return;

            var firstIns = method.Body.Instructions.First();
            var worker = method.Body.GetILProcessor();

            var hasPatchRef = assembly.MainModule.Import(typeof(Debug).GetMethod("Log", new Type[] { typeof(string) }));
            var current = InjectEmitHelper.InsertBefore(worker, firstIns, worker.Create(OpCodes.Ldstr, "Inject" + method.Name));
            current = InjectEmitHelper.InsertBefore(worker, firstIns, worker.Create(OpCodes.Call, hasPatchRef));
            InjectEmitHelper.ComputeOffsets(method.Body);
        }