示例#1
0
        public void Setup()
        {
            _classInstance = new Foo();

            Type refType       = typeof(IFoo <>);
            Type processorType = typeof(Foo);

            IEnumerable <Type> handledMessageTypes = processorType.GetInterfaces()
                                                     .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == refType)
                                                     .Select(i => i.GetGenericArguments().First());

            Type handledMessageType         = _messageInstance.GetType();
            Type concreteMessageHandlerType = refType.MakeGenericType(handledMessageType);

            MethodInfo method = processorType.GetInterfaceMap(concreteMessageHandlerType).TargetMethods
                                //we are just interested to cache the method ProcessMessageAsync
                                .Where(method => method.Name == nameof(IFoo <IMessage> .ProcessMessageAsync))
                                .First();

            Type delegateType = Expression.GetFuncType(handledMessageType, typeof(CancellationToken), typeof(ValueTask <bool>));

            _method           = method;
            _computedDelegate = Delegate.CreateDelegate(delegateType, _classInstance, method);

            _lambdaWrapper = createWrapperFunc(_method);
        }
示例#2
0
    public static string Serialize(Message1 obj)
    {
        XmlSerializer serializer = new XmlSerializer(obj.GetType());

        using (StringWriter writer = new StringWriter()) {
            serializer.Serialize(writer, obj);

            return(writer.ToString());
        }
    }
示例#3
0
 public async Task Handle(Message1 message)
 {
     _events.Enqueue($"Handling {message.GetType().Name}");
 }