示例#1
0
        private ITypeSerializerStrategy <TType> InternalCreate <TType>([NotNull] ISerializableTypeContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            //TODO: Refactor this. It's duplicated code
            //We should check if the specified a custom type serializer
            if (typeof(TType).GetTypeInfo().HasAttribute <IncludeCustomTypeSerializerAttribute>())
            {
                IncludeCustomTypeSerializerAttribute attri = typeof(TType).GetTypeInfo().GetCustomAttribute <IncludeCustomTypeSerializerAttribute>();

                if (!typeof(ITypeSerializerStrategy <TType>).GetTypeInfo().IsAssignableFrom(attri.TypeSerializerType))
                {
                    throw new InvalidOperationException($"Specified custom Type Serializer Type: {attri.TypeSerializerType} but did not implement {nameof(ITypeSerializerStrategy<TType>)}. Must implment that interface for custom serializers.");
                }

                ITypeSerializerStrategy <TType> serializer = Activator.CreateInstance(attri.TypeSerializerType) as ITypeSerializerStrategy <TType>;

                this.StrategyRegistry.RegisterType(typeof(TType), serializer);

                return(serializer);
            }


            DecoratorHandler handler = decoratorHandlers.First(h => h.CanHandle(context));

            ITypeSerializerStrategy <TType> strategy = handler.Create <TType>(context);

            if (strategy == null)
            {
                throw new InvalidOperationException($"Couldn't generate a strategy for Type: {context.TargetType} with Context: {context.BuiltContextKey?.ToString()}.");
            }

            //If the serializer is contextless we can register it with the general provider
            RegisterNewSerializerStrategy(context, strategy);

            return(strategy);
        }
        private ITypeSerializerStrategy <TTypeToRegister> GetTypeSerializerStrategyForType <TTypeToRegister>()
        {
            //We should check if the specified a custom type serializer
            if (typeof(TTypeToRegister).GetTypeInfo().HasAttribute <IncludeCustomTypeSerializerAttribute>())
            {
                IncludeCustomTypeSerializerAttribute attri = typeof(TTypeToRegister).GetTypeInfo().GetCustomAttribute <IncludeCustomTypeSerializerAttribute>();

                if (!typeof(ITypeSerializerStrategy <TTypeToRegister>).GetTypeInfo().IsAssignableFrom(attri.TypeSerializerType))
                {
                    throw new InvalidOperationException($"Specified custom Type Serializer Type: {attri.TypeSerializerType} but did not implement {nameof(ITypeSerializerStrategy<TTypeToRegister>)}. Must implment that interface for custom serializers.");
                }

                ITypeSerializerStrategy <TTypeToRegister> serializer = Activator.CreateInstance(attri.TypeSerializerType) as ITypeSerializerStrategy <TTypeToRegister>;

                this.serializerStorageService.RegisterType(typeof(TTypeToRegister), serializer);

                return(serializer);
            }

            //At this point this is a class marked with [WireDataContract] so we should assume and treat it as a complex type
            return(serializerStrategyFactoryService.Create <TTypeToRegister>(new TypeBasedSerializationContext(typeof(TTypeToRegister))) as ITypeSerializerStrategy <TTypeToRegister>);
        }