private static void AddType(Type type)
        {
            string     name   = null;
            MethodInfo reader = null;
            MethodInfo writer = null;

            foreach (var method in typeof(BinTypeExtensions).GetTypeInfo().GetMethods())
            {
                var attrib = method.GetCustomAttribute <BinTypeExtensionAttribute>(false);
                if (attrib == null || attrib.Type != type)
                {
                    continue;
                }

                name = attrib.Name;
                switch (attrib.Kind)
                {
                case TypeExtensionKind.Read: reader = method; break;

                case TypeExtensionKind.Write: writer = method; break;
                }
            }

            var description = new BinTypeDescription(type, name);
            var version     = new BinTypeVersion(0, 0);
            var process     = BinTypeProcess.Create(writer, reader);

            AddTypeImpl(description, version, process);
        }
        private static void AddStreamType <T>()
        {
            var        type   = typeof(T);
            MethodInfo reader = null;
            MethodInfo writer = null;
            MethodInfo skiper = null;

            foreach (var method in typeof(BinStreamExtensions).GetTypeInfo().GetMethods())
            {
                var attrib = method.GetCustomAttribute <BinStreamExtensionAttribute>(false);
                if (attrib == null || attrib.Type != type)
                {
                    continue;
                }

                switch (attrib.Kind)
                {
                case StreamExtensionKind.Read: reader = method; break;

                case StreamExtensionKind.Write: writer = method; break;

                case StreamExtensionKind.Skip: skiper = method; break;
                }
            }

            var description = new BinTypeDescription(type, type.Name);
            var version     = new BinTypeVersion(0, 0);
            var process     = BinTypeProcess.CreateStreamProcess(writer, reader, skiper);

            AddTypeImpl(description, version, process);
        }
        private static void AddArrayType()
        {
            var description = new BinTypeDescription(typeof(Array), ArrayToken);
            var version     = new BinTypeVersion(0, 0);

            AddTypeImpl(description, version, null);
        }
示例#4
0
 public static void AddType(BinTypeDescription description, BinTypeVersion version, BinTypeProcess process)
 {
     Locker.EnterWriteLock();
     try
     {
         AddTypeImpl(description, version, process);
     }
     finally
     {
         Locker.ExitWriteLock();
     }
 }
        public ArraySerializerTypeInfo(BinTypeDescription description, BinTypeVersion version, BinTypeProcess process)
            : base(description, version, process)
        {
            if (description.Type != typeof(Array))
            {
                throw new ArgumentException("Type must be an array.");
            }

            if (!string.Equals(description.TypeId, SerializerTypes.ArrayToken, StringComparison.Ordinal))
            {
                throw new ArgumentException("TypeId must be an array.");
            }
        }
示例#6
0
        public GenericSerializerTypeInfo(BinTypeDescription description, BinTypeVersion version, BinTypeProcess process)
            : base(description, version, process)
        {
            if (!Type.IsGenericType)
            {
                throw new ArgumentException("Type must be generic.");
            }

            if (!Type.IsGenericTypeDefinition)
            {
                throw new ArgumentException("Generic type must be opened.");
            }

            if (IsGenericTypeId(TypeId))
            {
                throw new ArgumentException("Type id must be declared as non generic");
            }
        }
示例#7
0
        private static void AddTypeImpl(BinTypeDescription description, BinTypeVersion version, BinTypeProcess process)
        {
            if (TypesById.ContainsKey(description.TypeId))
            {
                throw new InvalidOperationException(string.Format("TypeInfo with this id already exist {0} by type {1}.", description.TypeId, description.Type));
            }

            if (TypesByType.ContainsKey(description.Type))
            {
                throw new InvalidOperationException(string.Format("TypeInfo with this Type already exist {0} by id {1}.", description.Type, description.TypeId));
            }

            if (description.Type.IsArray)
            {
                throw new ArgumentException("Can't register array.");
            }

            if (process != null && !process.IsValid(description.Type))
            {
                throw new ArgumentException("Process is not valid");
            }

            SerializerTypeInfo typeInfo;

            if (description.Type == typeof(Array))
            {
                typeInfo = new ArraySerializerTypeInfo(description, version, process);
            }
            else if (description.Type.IsGenericType)
            {
                typeInfo = new GenericSerializerTypeInfo(description, version, process);
            }
            else
            {
                typeInfo = new SerializerTypeInfo(description, version, process);
            }

            TypesById.Add(description.TypeId, typeInfo);
            TypesByType.Add(description.Type, typeInfo);
        }
示例#8
0
        public static void AddTypesFrom(Assembly assembly)
        {
            Locker.EnterWriteLock();
            try
            {
                foreach (var type in assembly.DefinedTypes)
                {
                    var attribute = type.GetCustomAttribute <BinTypeAttribute>(false);
                    if (attribute != null)
                    {
                        var description = new BinTypeDescription(type, attribute.Id);
                        var version     = new BinTypeVersion(attribute.Version, attribute.MinSupportedVersion);

                        AddTypeImpl(description, version, null);
                    }
                }
            }
            finally
            {
                Locker.ExitWriteLock();
            }
        }
示例#9
0
 public static void AddType(BinTypeDescription description, BinTypeVersion version)
 {
     AddType(description, version, null);
 }