public static EventBrokerResolvedType ResolveType(IMemberMap<EventInfo> memberMap, TypeBuilder typeBuilder)
        {
            Type[] @params = null;
            Type delegateType = null;
            Type[] delegateParameters = null;
            var @event = memberMap.ContractMember;
            var declaringType = memberMap.ContractType;
            var invokeMethod = @event.GetInvokeMethod();
            var invokeReturnType = invokeMethod.ReturnType;
            var eventBrokerResolvedType = new EventBrokerResolvedType(@event) {
                DecalringType = declaringType
            };

            if (invokeMethod.GetDelegateParams(out @params, out delegateParameters)) {
                delegateType = @params.GetDelegateType(true);
                eventBrokerResolvedType.EventInterceptionContractArgs = @event.ToEventArgumentContract(@params);
                eventBrokerResolvedType.EventInterceptionArgs = declaringType.MakeGenericFunctionAspectArgsEvent(@params);
                eventBrokerResolvedType.EventBrokerBaseClassType = declaringType.MakeGenericEventBrokerBaseClassFunctionBinding(@params);
                eventBrokerResolvedType.EventBrokerInvokeDelegateType = typeof(Func<,>).MakeGenericType(new[] { eventBrokerResolvedType.EventInterceptionContractArgs, invokeReturnType });
            }
            else {
                delegateType = delegateParameters.GetDelegateType(false);
                eventBrokerResolvedType.EventInterceptionContractArgs = @event.ToEventArgumentContract(delegateParameters);
                eventBrokerResolvedType.EventInterceptionArgs = declaringType.MakeGenericActionAspectArgsEvent(delegateParameters);
                eventBrokerResolvedType.EventBrokerBaseClassType = declaringType.MakeGenericEventBrokerBaseClassActionType(delegateParameters);
                eventBrokerResolvedType.EventBrokerInvokeDelegateType = typeof(Action<>).MakeGenericType(new[] { eventBrokerResolvedType.EventInterceptionContractArgs });
            }

            eventBrokerResolvedType.Arguments = delegateParameters;
            eventBrokerResolvedType.EventBrokerFieldType = typeof(IEventBroker<>).MakeGenericType(delegateType);

            return eventBrokerResolvedType;
        }
示例#2
0
        private Type WeaveEventBrokerType(EventBrokerResolvedType eventBrokerResolvedType)
        {
            var name = "EventBrokerType_{0}".Fmt(Interlocked.Increment(ref eventBrokersTypeCounter)).ToUniqueName();
            var eventBrokerTypeBuilder = eventBrokerResolvedType.EventBrokerBaseClassType.DefineType(name, attributes: TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit);

            WeaveConstructor(eventBrokerTypeBuilder, eventBrokerResolvedType);
            interceptMethod = WeaveInterceptMethod(eventBrokerTypeBuilder, eventBrokerResolvedType);
            WeaveSubscribeImpl(eventBrokerTypeBuilder, eventBrokerResolvedType);
            WeaveUnsubscribeImpl(eventBrokerTypeBuilder, eventBrokerResolvedType);

            return(eventBrokerTypeBuilder.CreateType());
        }
示例#3
0
        private Type WeaveEventBrokerType(EventBrokerResolvedType eventBrokerResolvedType)
        {
            var name = "EventBrokerType_{0}".Fmt(Interlocked.Increment(ref eventBrokersTypeCounter)).ToUniqueName();
            var eventBrokerTypeBuilder = eventBrokerResolvedType.EventBrokerBaseClassType.DefineType(name, attributes: TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit);

            WeaveConstructor(eventBrokerTypeBuilder, eventBrokerResolvedType);
            interceptMethod = WeaveInterceptMethod(eventBrokerTypeBuilder, eventBrokerResolvedType);
            WeaveSubscribeImpl(eventBrokerTypeBuilder, eventBrokerResolvedType);
            WeaveUnsubscribeImpl(eventBrokerTypeBuilder, eventBrokerResolvedType);

            return eventBrokerTypeBuilder.CreateType();
        }
        public EventBrokerFieldTypeDefinition(EventInfo @event, EventBrokerResolvedType eventBrokerResolvedType, TypeBuilder typeBuilder, Type eventBrokerGeneratedType)
        {
            string fieldName = null;

            Event                         = @event;
            EventBrokerType               = eventBrokerGeneratedType;
            InvokeMethodName              = "Invoke{0}".Fmt(@event.Name);
            EventInterceptionArgs         = eventBrokerResolvedType.EventInterceptionArgs;
            EventBrokerDelegateType       = eventBrokerResolvedType.EventBrokerInvokeDelegateType;
            EventInterceptionContractArgs = eventBrokerResolvedType.EventInterceptionContractArgs;
            fieldName                     = "EventBroker_{0}".Fmt(Interlocked.Increment(ref eventBrokersCounter)).ToUniqueName();
            fieldBuilderDefinition        = new FieldBuilderDefinition(eventBrokerResolvedType.EventBrokerFieldType, typeBuilder, fieldName, fieldAttributes);
        }
        public EventBrokerFieldTypeDefinition(EventInfo @event, EventBrokerResolvedType eventBrokerResolvedType, TypeBuilder typeBuilder, Type eventBrokerGeneratedType)
        {
            string fieldName = null;

            Event = @event;
            EventBrokerType = eventBrokerGeneratedType;
            InvokeMethodName = "Invoke{0}".Fmt(@event.Name);
            EventInterceptionArgs = eventBrokerResolvedType.EventInterceptionArgs;
            EventBrokerDelegateType = eventBrokerResolvedType.EventBrokerInvokeDelegateType;
            EventInterceptionContractArgs = eventBrokerResolvedType.EventInterceptionContractArgs;
            fieldName = "EventBroker_{0}".Fmt(Interlocked.Increment(ref eventBrokersCounter)).ToUniqueName();
            fieldBuilderDefinition = new FieldBuilderDefinition(eventBrokerResolvedType.EventBrokerFieldType, typeBuilder, fieldName, fieldAttributes);
        }
示例#6
0
        private MethodBuilder WeaveInterceptMethod(TypeBuilder typeBuilder, EventBrokerResolvedType eventBrokerResolvedType)
        {
            var invokeMethod       = eventBrokerResolvedType.EventBrokerInvokeDelegateType.GetInvokeMethod();
            var interceptMethod    = typeBuilder.DefineMethod("Intercept", interceptMethodAttrs, invokeMethod.ReturnType, eventBrokerResolvedType.Arguments);
            var ilGenerator        = interceptMethod.GetILGenerator();
            var onEventFiredMethod = eventBrokerResolvedType.EventBrokerBaseClassType.GetMethod("OnEventFired", BindingFlags.NonPublic | BindingFlags.Instance);

            ilGenerator.EmitLoadArg(0);
            eventBrokerResolvedType.Arguments.ForEach(1, (arg, i) => ilGenerator.EmitLoadArg(i));
            ilGenerator.Emit(OpCodes.Call, onEventFiredMethod);
            ilGenerator.Emit(OpCodes.Ret);

            return(interceptMethod);
        }
示例#7
0
        private void WeaveConstructor(TypeBuilder typeBuilder, EventBrokerResolvedType eventBrokerResolvedType)
        {
            ConstructorBuilder ctor        = null;
            ILGenerator        ilGenerator = null;
            var ctorArgs = new[] { eventBrokerResolvedType.DecalringType, typeof(EventInfo), eventBrokerResolvedType.EventBrokerInvokeDelegateType };
            var baseCtor = eventBrokerResolvedType.EventBrokerBaseClassType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, Type.DefaultBinder, ctorArgs, null);

            ctor        = typeBuilder.DefineConstructor(ctorAttrs, CallingConventions.Standard | CallingConventions.HasThis, ctorArgs);
            ilGenerator = ctor.GetILGenerator();
            ilGenerator.EmitLoadArg(0);
            ctorArgs.ForEach(1, (arg, i) => ilGenerator.EmitLoadArg(i));
            ilGenerator.Emit(OpCodes.Call, baseCtor);
            ilGenerator.Emit(OpCodes.Ret);
        }
示例#8
0
        private void WeaveConstructor(TypeBuilder typeBuilder, EventBrokerResolvedType eventBrokerResolvedType)
        {
            ConstructorBuilder ctor = null;
            ILGenerator ilGenerator = null;
            var ctorArgs = new[] { eventBrokerResolvedType.DecalringType, typeof(EventInfo), eventBrokerResolvedType.EventBrokerInvokeDelegateType };
            var baseCtor = eventBrokerResolvedType.EventBrokerBaseClassType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, Type.DefaultBinder, ctorArgs, null);

            ctor = typeBuilder.DefineConstructor(ctorAttrs, CallingConventions.Standard | CallingConventions.HasThis, ctorArgs);
            ilGenerator = ctor.GetILGenerator();
            ilGenerator.EmitLoadArg(0);
            ctorArgs.ForEach(1, (arg, i) => ilGenerator.EmitLoadArg(i));
            ilGenerator.Emit(OpCodes.Call, baseCtor);
            ilGenerator.Emit(OpCodes.Ret);
        }
示例#9
0
        private void WeaveSubscriptionMethod(TypeBuilder typeBuilder, EventBrokerResolvedType eventBrokerResolvedType, MethodInfo eventHandlerMethod, string methodName)
        {
            var instanceField       = eventBrokerResolvedType.EventBrokerBaseClassType.GetField("instance", BindingFlags.NonPublic | BindingFlags.Instance);
            var subscribeImplMethod = typeBuilder.DefineMethod(methodName, subscribtionsMethodsAttrs, typeof(void), Type.EmptyTypes);
            var eventHanlderCtor    = eventBrokerResolvedType.Event.EventHandlerType.GetConstructor(new[] { typeof(object), typeof(IntPtr) });
            var ilGenerator         = subscribeImplMethod.GetILGenerator();

            ilGenerator.EmitLoadArg(0);
            ilGenerator.Emit(OpCodes.Ldfld, instanceField);
            ilGenerator.EmitLoadArg(0);
            ilGenerator.Emit(OpCodes.Ldftn, interceptMethod);
            ilGenerator.Emit(OpCodes.Newobj, eventHanlderCtor);
            ilGenerator.Emit(OpCodes.Callvirt, eventHandlerMethod);
            ilGenerator.Emit(OpCodes.Ret);
        }
        public static EventBrokerResolvedType ResolveType(IMemberMap <EventInfo> memberMap, TypeBuilder typeBuilder)
        {
            Type[] @params      = null;
            Type   delegateType = null;

            Type[] delegateParameters      = null;
            var    @event                  = memberMap.ContractMember;
            var    declaringType           = memberMap.ContractType;
            var    invokeMethod            = @event.GetInvokeMethod();
            var    invokeReturnType        = invokeMethod.ReturnType;
            var    eventBrokerResolvedType = new EventBrokerResolvedType(@event)
            {
                DecalringType = declaringType
            };

            if (invokeMethod.GetDelegateParams(out @params, out delegateParameters))
            {
                delegateType = @params.GetDelegateType(true);
                eventBrokerResolvedType.EventInterceptionContractArgs = @event.ToEventArgumentContract(@params);
                eventBrokerResolvedType.EventInterceptionArgs         = declaringType.MakeGenericFunctionAspectArgsEvent(@params);
                eventBrokerResolvedType.EventBrokerBaseClassType      = declaringType.MakeGenericEventBrokerBaseClassFunctionBinding(@params);
                eventBrokerResolvedType.EventBrokerInvokeDelegateType = typeof(Func <,>).MakeGenericType(new[] { eventBrokerResolvedType.EventInterceptionContractArgs, invokeReturnType });
            }
            else
            {
                delegateType = delegateParameters.GetDelegateType(false);
                eventBrokerResolvedType.EventInterceptionContractArgs = @event.ToEventArgumentContract(delegateParameters);
                eventBrokerResolvedType.EventInterceptionArgs         = declaringType.MakeGenericActionAspectArgsEvent(delegateParameters);
                eventBrokerResolvedType.EventBrokerBaseClassType      = declaringType.MakeGenericEventBrokerBaseClassActionType(delegateParameters);
                eventBrokerResolvedType.EventBrokerInvokeDelegateType = typeof(Action <>).MakeGenericType(new[] { eventBrokerResolvedType.EventInterceptionContractArgs });
            }

            eventBrokerResolvedType.Arguments            = delegateParameters;
            eventBrokerResolvedType.EventBrokerFieldType = typeof(IEventBroker <>).MakeGenericType(delegateType);

            return(eventBrokerResolvedType);
        }
示例#11
0
 private void WeaveUnsubscribeImpl(TypeBuilder typeBuilder, EventBrokerResolvedType eventBrokerResolvedType)
 {
     WeaveSubscriptionMethod(typeBuilder, eventBrokerResolvedType, eventBrokerResolvedType.Event.GetRemoveMethod(), "UnsubscribeImpl");
 }
示例#12
0
        private MethodBuilder WeaveInterceptMethod(TypeBuilder typeBuilder, EventBrokerResolvedType eventBrokerResolvedType)
        {
            var invokeMethod = eventBrokerResolvedType.EventBrokerInvokeDelegateType.GetInvokeMethod();
            var interceptMethod = typeBuilder.DefineMethod("Intercept", interceptMethodAttrs, invokeMethod.ReturnType, eventBrokerResolvedType.Arguments);
            var ilGenerator = interceptMethod.GetILGenerator();
            var onEventFiredMethod = eventBrokerResolvedType.EventBrokerBaseClassType.GetMethod("OnEventFired", BindingFlags.NonPublic | BindingFlags.Instance);

            ilGenerator.EmitLoadArg(0);
            eventBrokerResolvedType.Arguments.ForEach(1, (arg, i) => ilGenerator.EmitLoadArg(i));
            ilGenerator.Emit(OpCodes.Call, onEventFiredMethod);
            ilGenerator.Emit(OpCodes.Ret);

            return interceptMethod;
        }
示例#13
0
 private void WeaveUnsubscribeImpl(TypeBuilder typeBuilder, EventBrokerResolvedType eventBrokerResolvedType)
 {
     WeaveSubscriptionMethod(typeBuilder, eventBrokerResolvedType, eventBrokerResolvedType.Event.GetRemoveMethod(), "UnsubscribeImpl");
 }
示例#14
0
        private void WeaveSubscriptionMethod(TypeBuilder typeBuilder, EventBrokerResolvedType eventBrokerResolvedType, MethodInfo eventHandlerMethod, string methodName)
        {
            var instanceField = eventBrokerResolvedType.EventBrokerBaseClassType.GetField("instance", BindingFlags.NonPublic | BindingFlags.Instance);
            var subscribeImplMethod = typeBuilder.DefineMethod(methodName, subscribtionsMethodsAttrs, typeof(void), Type.EmptyTypes);
            var eventHanlderCtor = eventBrokerResolvedType.Event.EventHandlerType.GetConstructor(new[] { typeof(object), typeof(IntPtr) });
            var ilGenerator = subscribeImplMethod.GetILGenerator();

            ilGenerator.EmitLoadArg(0);
            ilGenerator.Emit(OpCodes.Ldfld, instanceField);
            ilGenerator.EmitLoadArg(0);
            ilGenerator.Emit(OpCodes.Ldftn, interceptMethod);
            ilGenerator.Emit(OpCodes.Newobj, eventHanlderCtor);
            ilGenerator.Emit(OpCodes.Callvirt, eventHandlerMethod);
            ilGenerator.Emit(OpCodes.Ret);
        }