Beispiel #1
0
        public override void Implement(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder typeBuilder)
        {
            var il = this.AddMethod.GetILGenerator();

            ProxyMethodHelper.GenerateBody(il, this.implementation.PropertyGetMethod, this.baseEvent.GetAddMethod());

            var il2 = this.RemoveMethod.GetILGenerator();

            ProxyMethodHelper.GenerateBody(il2, this.implementation.PropertyGetMethod, this.baseEvent.GetRemoveMethod());
        }
Beispiel #2
0
        protected override void Implement(DynamicTypeBuilder config, TypeBuilder typeBuilder, ILGenerator methodBody)
        {
            var prop = typeof(DynamicObjectBase).GetProperty("DelegateRegister", BindingFlags.NonPublic | BindingFlags.Instance);

            var getEnumerator = typeof(IEnumerable <Delegate>).GetMethod("GetEnumerator");

            var moveNext   = typeof(IEnumerator).GetMethod("MoveNext");
            var getCurrent = typeof(IEnumerator <Delegate>).GetProperty("Current").GetGetMethod();

            var getoraddMethod = typeof(ConcurrentDictionary <string, List <Delegate> >).GetMethod("GetOrAdd", new[] { typeof(string), typeof(List <Delegate>) });

            var subscribed = methodBody.DeclareLocal(typeof(List <Delegate>));
            var enumerator = methodBody.DeclareLocal(typeof(IEnumerator <Delegate>));

            methodBody.Emit(OpCodes.Ldarg_0);
            methodBody.EmitCall(OpCodes.Callvirt, prop.GetGetMethod(true), null);
            methodBody.Emit(OpCodes.Ldstr, this.MemberName);
            methodBody.Emit(OpCodes.Newobj, typeof(List <Delegate>).GetConstructor(new Type[] { }));
            methodBody.EmitCall(OpCodes.Callvirt, getoraddMethod, null);
            methodBody.Emit(OpCodes.Stloc_S, subscribed);
            methodBody.Emit(OpCodes.Ldloc_S, subscribed);
            methodBody.EmitCall(OpCodes.Callvirt, getEnumerator, null);
            methodBody.Emit(OpCodes.Stloc_S, enumerator);

            var exitLabel = methodBody.DefineLabel();
            var loopStart = methodBody.DefineLabel();

            methodBody.MarkLabel(loopStart);
            methodBody.Emit(OpCodes.Ldloc_S, enumerator);
            methodBody.EmitCall(OpCodes.Callvirt, moveNext, null);
            methodBody.Emit(OpCodes.Brfalse, exitLabel);

            var arg = this.ArgumentTypes.FirstOrDefault();

            var actionType = typeof(Action);

            if (arg != null)
            {
                actionType = typeof(Action <>).MakeGenericType(arg);
            }

            methodBody.Emit(OpCodes.Ldloc_S, enumerator);
            methodBody.EmitCall(OpCodes.Callvirt, getCurrent, null);
            methodBody.Emit(OpCodes.Castclass, actionType);
            if (arg != null)
            {
                methodBody.Emit(OpCodes.Ldarg_1);
            }
            methodBody.EmitCall(OpCodes.Callvirt, actionType.GetMethod("Invoke"), null);

            methodBody.Emit(OpCodes.Br_S, loopStart);

            methodBody.MarkLabel(exitLabel);
            methodBody.Emit(OpCodes.Ret);
        }
        public override void Implement(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder typeBuilder)
        {
            var il = this.PropertyGetMethod.GetILGenerator();

            ProxyMethodHelper.GenerateBody(il, this.implementation.PropertyGetMethod, this.baseProperty.GetGetMethod());

            if (!this.IsReadOnly)
            {
                var il2 = this.PropertySetMethod.GetILGenerator();
                ProxyMethodHelper.GenerateBody(il2, this.implementation.PropertyGetMethod, this.baseProperty.GetSetMethod());
            }
        }
Beispiel #4
0
        public override void Implement(DynamicTypeBuilder config, TypeBuilder typeBuilder)
        {
            #region GetMethodILCode
            var baseProperty = typeBuilder.BaseType.GetProperty(this.MemberName);

            var getMethodIlGenerator = PropertyGetMethod.GetILGenerator();
            if (IsOverride)
            {
                getMethodIlGenerator.Emit(OpCodes.Ldarg_0);
                getMethodIlGenerator.Emit(OpCodes.Call, baseProperty.GetGetMethod());
                getMethodIlGenerator.Emit(OpCodes.Ret);
            }
            else
            {
                getMethodIlGenerator.Emit(OpCodes.Ldarg_0);
                getMethodIlGenerator.Emit(OpCodes.Ldfld, this.BackingField);
                getMethodIlGenerator.Emit(OpCodes.Ret);
            }
            #endregion

            #region SetMethodILCode
            if (!IsReadOnly)
            {
                var setMethodIlGenerator = PropertySetMethod.GetILGenerator();

                Label returnLabel;
                Label originalReturn = returnLabel = setMethodIlGenerator.DefineLabel();

                foreach (var interceptor in this.SetInterceptors)
                {
                    interceptor.Intercept(this, PropertySetMethod, setMethodIlGenerator, ref returnLabel);
                }

                if (IsOverride)
                {
                    setMethodIlGenerator.Emit(OpCodes.Ldarg_0);
                    setMethodIlGenerator.Emit(OpCodes.Ldarg_1);
                    setMethodIlGenerator.Emit(OpCodes.Call, baseProperty.GetSetMethod());
                }
                else
                {
                    setMethodIlGenerator.Emit(OpCodes.Ldarg_0);
                    setMethodIlGenerator.Emit(OpCodes.Ldarg_1);
                    setMethodIlGenerator.Emit(OpCodes.Stfld, BackingField);
                }
                setMethodIlGenerator.Emit(OpCodes.Br_S, returnLabel);

                setMethodIlGenerator.MarkLabel(originalReturn);
                setMethodIlGenerator.Emit(OpCodes.Ret);
            }
            #endregion
        }
Beispiel #5
0
        public override void Implement(DynamicTypeBuilder config, TypeBuilder typeBuilder)
        {
            var addMethodIL = AddMethod.GetILGenerator();

            #region AddMethodIL
            GenerateHandlerMethods(BackingField, addMethodIL, true);

            #endregion

            var removeMethodIL = RemoveMethod.GetILGenerator();
            #region RemoveMethodIL
            GenerateHandlerMethods(BackingField, removeMethodIL, false);

            #endregion
        }
Beispiel #6
0
 public override void Implement(DynamicTypeBuilder config, System.Reflection.Emit.TypeBuilder typeBuilder)
 {
 }
Beispiel #7
0
 public abstract void Implement(DynamicTypeBuilder config, TypeBuilder typeBuilder);
Beispiel #8
0
 protected abstract void Implement(DynamicTypeBuilder config, TypeBuilder typeBuilder, ILGenerator il);
Beispiel #9
0
        public override sealed void Implement(DynamicTypeBuilder config, TypeBuilder typeBuilder)
        {
            Implement(config, typeBuilder, ImplementationMethod.GetILGenerator());
            var il = this.Method.GetILGenerator();
            var parameterArrayVar = il.DeclareLocal(typeof(object[]));

            bool isAsync = false;

            if (this.MemberType != null && typeof(Task).IsAssignableFrom(TypeLookup(this.MemberType)))
            {
                isAsync = true;
            }

            var ctxType = isAsync ? typeof(AsyncInterceptionContext) : typeof(InterceptionContext);

            LocalBuilder    ctxVar = il.DeclareLocal(ctxType);
            ConstructorInfo icCtor = ctxType.GetConstructors().First();

            ConstructorInfo delegateCtor = null;

            if (this.MemberType == null)
            {
                Assembly assembly = null;
                if (this.ArgumentTypes.Count < 9)
                {
                    assembly = typeof(Action).Assembly;
                }
                else
                {
                    assembly = typeof(Enumerable).Assembly;
                }

                var type = assembly.GetType(string.Format("System.Action{0}", this.ArgumentTypes.Count > 0 ? "`" + this.argumentTypes.Count.ToString() : string.Empty));
                delegateCtor = type.GetConstructor(new[] { typeof(object), typeof(IntPtr) });
                if (argumentTypes.Count > 0)
                {
                    type = type.MakeGenericType(TypeLookup(this.ArgumentTypes).ToArray());
                }

                if (this.argumentTypes.OfType <GenericType>().Any())
                {
                    delegateCtor = TypeBuilder.GetConstructor(type, delegateCtor);
                }
                else
                {
                    delegateCtor = type.GetConstructor(new[] { typeof(object), typeof(IntPtr) });
                }
            }
            else
            {
                Assembly assembly = null;
                if (this.ArgumentTypes.Count < 9)
                {
                    assembly = typeof(Action).Assembly;
                }
                else
                {
                    assembly = typeof(Enumerable).Assembly;
                }
                var type = assembly.GetType(string.Format("System.Func`{0}", this.ArgumentTypes.Count + 1));
                delegateCtor = type.GetConstructor(new[] { typeof(object), typeof(IntPtr) });
                type         = type.MakeGenericType(TypeLookup(this.ArgumentTypes).Concat(new[] { TypeLookup(this.MemberType) }).ToArray());

                if (this.argumentTypes.OfType <GenericType>().Any() || this.MemberType is GenericType)
                {
                    delegateCtor = TypeBuilder.GetConstructor(type, delegateCtor);
                }
                else
                {
                    delegateCtor = type.GetConstructor(new[] { typeof(object), typeof(IntPtr) });
                }
            }

            var returnLabel = il.DefineLabel();

            il.Emit(OpCodes.Ldc_I4, this.ArgumentTypes.Count);
            il.Emit(OpCodes.Newarr, typeof(object));
            il.Emit(OpCodes.Stloc, parameterArrayVar);

            for (int i = 1; i <= this.ArgumentTypes.Count; i++)
            {
                il.Emit(OpCodes.Ldloc, parameterArrayVar);
                il.Emit(OpCodes.Ldc_I4, i - 1);
                il.Emit(OpCodes.Ldarg, i);
                var type = TypeLookup(this.argumentTypes[i - 1]);
                il.Emit(OpCodes.Box, type);
                il.Emit(OpCodes.Stelem_Ref);
            }
            il.Emit(OpCodes.Ldarg_0);
            il.Emit(OpCodes.Ldstr, this.MemberName);

            il.Emit(OpCodes.Ldarg_0);
            if (this.ImplementationMethod.IsGenericMethod)
            {
                il.Emit(OpCodes.Ldftn, this.ImplementationMethod.MakeGenericMethod(this.GenericParameters.ToArray()));
            }
            else
            {
                il.Emit(OpCodes.Ldftn, this.ImplementationMethod);
            }
            il.Emit(OpCodes.Newobj, delegateCtor);
            il.Emit(OpCodes.Ldloc, parameterArrayVar);
            il.Emit(OpCodes.Newobj, icCtor);
            il.Emit(OpCodes.Stloc, ctxVar);

            List <MethodInfo> interceptors = new List <MethodInfo>();

            if (isAsync)
            {
                interceptors = typeBuilder.BaseType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                               .Where(p => !p.IsPrivate && p.GetCustomAttributes(typeof(MethodInterceptorAttribute), true).Any() && p.GetParameters().Count() == 1 && p.GetParameters().First().ParameterType == typeof(AsyncInterceptionContext) && p.ReturnType == typeof(Task))
                               .ToList();
            }
            else
            {
                interceptors = typeBuilder.BaseType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                               .Where(p => !p.IsPrivate && p.GetCustomAttributes(typeof(MethodInterceptorAttribute), true).Any() && p.GetParameters().Count() == 1 && p.GetParameters().First().ParameterType == typeof(InterceptionContext))
                               .ToList();
            }

            var registerMethod = ctxType.GetMethod("RegisterInterceptor");

            foreach (var item in interceptors)
            {
                il.Emit(OpCodes.Ldloc, ctxVar);
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Ldftn, item);
                if (isAsync)
                {
                    il.Emit(OpCodes.Newobj, typeof(Func <AsyncInterceptionContext, Task>).GetConstructor(new[] { typeof(object), typeof(IntPtr) }));
                }
                else
                {
                    il.Emit(OpCodes.Newobj, typeof(Action <InterceptionContext>).GetConstructor(new[] { typeof(object), typeof(IntPtr) }));
                }
                il.Emit(OpCodes.Ldc_I4, (int)item.GetCustomAttributes(typeof(MethodInterceptorAttribute), true).OfType <MethodInterceptorAttribute>().First().InterceptionMode);
                if (isAsync)
                {
                    il.Emit(OpCodes.Newobj, typeof(DelegateAsyncMethodInterceptor).GetConstructors().First());
                }
                else
                {
                    il.Emit(OpCodes.Newobj, typeof(DelegateMethodInterceptor).GetConstructors().First());
                }
                il.EmitCall(OpCodes.Callvirt, registerMethod, null);
            }

            if (isAsync)
            {
                var taskType = TypeLookup(this.MemberType).IsGenericType ? TypeLookup(this.MemberType).GetGenericArguments().First() : typeof(object);

                il.Emit(OpCodes.Ldloc, ctxVar);
                il.EmitCall(OpCodes.Callvirt, ctxType.GetMethod("ExecuteAsync").MakeGenericMethod(taskType), null);
            }
            else
            {
                il.Emit(OpCodes.Ldloc, ctxVar);
                il.EmitCall(OpCodes.Callvirt, typeof(InterceptionContext).GetMethod("Execute"), null);
                if (this.MemberType != null)
                {
                    il.Emit(OpCodes.Ldloc, ctxVar);
                    il.Emit(OpCodes.Callvirt, typeof(InterceptionContext).GetProperty("Result").GetGetMethod());
                    var returnType = TypeLookup(this.MemberType);
                    if (returnType.IsValueType || returnType.IsGenericParameter)
                    {
                        il.Emit(OpCodes.Unbox_Any, TypeLookup(this.MemberType));
                    }
                    else
                    {
                        il.Emit(OpCodes.Castclass, returnType);
                    }
                }
            }
            //else {

            //    il.Emit(OpCodes.Ldarg_0);
            //    for (int i = 1; i <= this.ArgumentTypes.Count(); i++) {
            //        il.Emit(OpCodes.Ldarg, i);
            //    }
            //    if (this.ImplementationMethod.IsGenericMethod) {
            //        il.EmitCall(OpCodes.Call, this.ImplementationMethod.MakeGenericMethod(this.GenericParameters.ToArray()), null);
            //    } else {
            //        il.EmitCall(OpCodes.Call, this.ImplementationMethod, null);
            //    }
            //}
            il.Emit(OpCodes.Ret);
        }
Beispiel #10
0
 public virtual void OnBuilderInitialized(DynamicTypeBuilder dynamicTypeBuilder)
 {
 }
Beispiel #11
0
 public virtual void OnMembersCreated(DynamicTypeBuilder dynamicTypeBuilder)
 {
 }