Пример #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            Type proxyType = InterfaceImplementationBuilder.GetProxyFor(objectType);

            object obj = FastActivator.Create(proxyType);

            serializer.Populate(reader, obj);

            return(obj);
        }
Пример #2
0
        private static ObjectBinder CreateBinderFor(Type type)
        {
            if (type.Implements <Channel>())
            {
                Type[] genericType = type.GetGenericArguments();

                return((ObjectBinder)FastActivator.Create(typeof(ChannelBinder <>).MakeGenericType(genericType)));
            }

            if (type.IsEnum)
            {
                return((ObjectBinder)FastActivator.Create(typeof(EnumBinder <>).MakeGenericType(type)));
            }

            if (typeof(IEnumerable).IsAssignableFrom(type) && type != typeof(string))
            {
                if (type.IsArray)
                {
                    return((ObjectBinder)FastActivator.Create(typeof(ArrayBinder <>).MakeGenericType(type.GetElementType())));
                }
                if (type.IsGenericType)
                {
                    Type   genericTypeDefinition = type.GetGenericTypeDefinition();
                    Type[] arguments             = type.GetGenericArguments();
                    if (genericTypeDefinition == typeof(IList <>) || genericTypeDefinition == typeof(List <>))
                    {
                        return((ObjectBinder)FastActivator.Create(typeof(ListBinder <>).MakeGenericType(arguments)));
                    }

//					if (genericTypeDefinition == typeof (IDictionary<,>) || genericTypeDefinition == typeof (Dictionary<,>))
//					{
//						return (ObjectBinder) FastActivator.Create(typeof (DictionaryBinder<,>).MakeGenericType(arguments));
//					}
                }

                throw new NotSupportedException("Unsupported enumeration type: " + type.FullName);
            }

            Type binderType;

            if (type.IsInterface)
            {
                Type proxyType = InterfaceImplementationBuilder.GetProxyFor(type);
                binderType = typeof(FastObjectBinder <>).MakeGenericType(proxyType);
            }
            else
            {
                binderType = typeof(FastObjectBinder <>).MakeGenericType(type);
            }

            return((ObjectBinder)FastActivator.Create(binderType));
        }
Пример #3
0
        public static void Respond <TRequest>(this Request <TRequest> request)
        {
            if (!typeof(TRequest).IsInterface)
            {
                throw new ArgumentException("Default Implementations can only be created for interfaces");
            }

            Type requestImplType = InterfaceImplementationBuilder.GetProxyFor(typeof(TRequest));

            var responseImpl = new ResponseImpl <TRequest, TRequest>(request, request.Body);

            request.ResponseChannel.Send <Response <TRequest, TRequest> >(responseImpl);
        }
Пример #4
0
        /// <summary>
        ///   Sends an uninitialized interface implementation
        /// </summary>
        /// <typeparam name = "T">The request message type, which must be an interface</typeparam>
        /// <param name = "channel">The target channel</param>
        public static void Send <T>(this UntypedChannel channel)
        {
            if (!typeof(T).IsInterface)
            {
                throw new ArgumentException("Default Implementations can only be created for interfaces");
            }

            Type requestImplType = InterfaceImplementationBuilder.GetProxyFor(typeof(T));

            var message = (T)FastActivator.Create(requestImplType);

            channel.Send(message);
        }
Пример #5
0
        static ObjectBinder CreateBinderFor(Type type)
        {
            Type underlyingType = Nullable.GetUnderlyingType(type);

            if (underlyingType != null)
            {
                ObjectBinder underlyingBinder = _typeBinders[underlyingType];
                return((ObjectBinder)FastActivator.Create(typeof(NullableBinder <>), new[] { underlyingType },
                                                          new object[] { underlyingBinder }));
            }

            if (type.IsEnum)
            {
                return((ObjectBinder)FastActivator.Create(typeof(EnumBinder <>), new[] { type }));
            }

            if (typeof(IEnumerable).IsAssignableFrom(type) && type != typeof(string))
            {
                if (type.IsArray)
                {
                    return((ObjectBinder)FastActivator.Create(typeof(ArrayBinder <>), new[] { type.GetElementType() }));
                }

                if (type.IsGenericType)
                {
                    Type   genericTypeDefinition = type.GetGenericTypeDefinition();
                    Type[] arguments             = type.GetGenericArguments();
                    if (genericTypeDefinition == typeof(IList <>) || genericTypeDefinition == typeof(List <>))
                    {
                        return((ObjectBinder)FastActivator.Create(typeof(ListBinder <>), arguments));
                    }

                    // TODO handle dictionary as well
                }

                throw new NotSupportedException("Unsupported enumeration type: " + type.FullName);
            }

            if (type.IsInterface)
            {
                Type proxyType = InterfaceImplementationBuilder.GetProxyFor(type);

                return((ObjectBinder)FastActivator.Create(typeof(FastObjectBinder <>), new[] { proxyType }));
            }

            return((ObjectBinder)FastActivator.Create(typeof(FastObjectBinder <>), new[] { type }));
        }
Пример #6
0
        /// <summary>
        ///   Sends an uninitialized interface implementation as a request
        /// </summary>
        /// <typeparam name = "TRequest">The request message type, which must be an interface</typeparam>
        /// <param name = "channel">The target channel</param>
        /// <param name = "responseChannel">The channel where responses should be sent</param>
        public static Request <TRequest> Request <TRequest>(this UntypedChannel channel,
                                                            UntypedChannel responseChannel)
        {
            if (!typeof(TRequest).IsInterface)
            {
                throw new ArgumentException("Default Implementations can only be created for interfaces");
            }

            Type requestImplType = InterfaceImplementationBuilder.GetProxyFor(typeof(TRequest));

            var request = (TRequest)FastActivator.Create(requestImplType);

            var requestImpl = new RequestImpl <TRequest>(responseChannel, request);

            channel.Send <Request <TRequest> >(requestImpl);

            return(requestImpl);
        }
Пример #7
0
        public bool TryConvert <T>(out T message)
            where T : class
        {
            if (typeof(T) == typeof(JToken))
            {
                message = _token as T;
                return(true);
            }

            object existing;

            if (_mapped.TryGetValue(typeof(T), out existing))
            {
                message = (T)existing;
                return(message != null);
            }

            string typeUrn = new MessageUrn(typeof(T)).ToString();

            if (_supportedTypes.Any(typeUrn.Equals))
            {
                object obj;
                Type   deserializeType = typeof(T);
                if (deserializeType.IsInterface && deserializeType.IsAllowedMessageType())
                {
                    deserializeType = InterfaceImplementationBuilder.GetProxyFor(deserializeType);
                }

                using (var jsonReader = new JTokenReader(_token))
                {
                    obj = _serializer.Deserialize(jsonReader, deserializeType);
                }

                _mapped[typeof(T)] = obj;

                message = (T)obj;
                return(true);
            }

            _mapped[typeof(T)] = null;

            message = null;
            return(false);
        }
Пример #8
0
        public bool TryConvert <T>(out T message)
            where T : class
        {
            object existing;

            if (_mapped.TryGetValue(typeof(T), out existing))
            {
                message = (T)existing;
                return(message != null);
            }

            string typeUrn = new MessageUrn(typeof(T)).ToString();

            if (_supportedTypes.Any(typeUrn.Equals))
            {
                object obj;
                if (typeof(T).IsInterface && typeof(T).IsAllowedMessageType())
                {
                    Type proxyType = InterfaceImplementationBuilder.GetProxyFor(typeof(T));

                    obj = FastActivator.Create(proxyType);

                    UsingReader(jsonReader => _serializer.Populate(jsonReader, obj));
                }
                else
                {
                    obj = FastActivator <T> .Create();

                    UsingReader(jsonReader => _serializer.Populate(jsonReader, obj));
                }

                _mapped[typeof(T)] = obj;

                message = (T)obj;
                return(true);
            }

            _mapped[typeof(T)] = null;

            message = null;
            return(false);
        }
Пример #9
0
        public void Should_be_able_to_suck_out_an_interface()
        {
            Envelope result;

            using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(_body), false))
                using (var reader = new StreamReader(memoryStream))
                    using (var jsonReader = new JsonTextReader(reader))
                    {
                        result = _deserializer.Deserialize <Envelope>(jsonReader);
                    }

            using (var jsonReader = new JTokenReader(result.Message as JToken))
            {
                Type proxyType = InterfaceImplementationBuilder.GetProxyFor(typeof(MessageA));
                var  message   = (MessageA)FastActivator.Create(proxyType);

                _serializer.Populate(jsonReader, message);

                message.Name.ShouldEqual("Joe");
            }
        }
Пример #10
0
        FastTextTypeSerializer CreateObjectSerializerForInterface(Type type)
        {
            Type proxyType = InterfaceImplementationBuilder.GetProxyFor(type);

            return(CreateObjectSerializerFor(proxyType));
        }