Example #1
0
		Dictionary<Type, TypeData> GenerateTypeData(IEnumerable<Type> rootTypes)
		{
			var map = new Dictionary<Type, TypeData>();
			var stack = new Stack<Type>(PrimitivesSerializer.GetSupportedTypes().Concat(rootTypes));

			stack.Push(typeof(object));

			// TypeID 0 is reserved for null
			ushort typeID = 1;

			while (stack.Count > 0)
			{
				var type = stack.Pop();

				if (map.ContainsKey(type))
					continue;

				if (type.IsAbstract || type.IsInterface)
					continue;

				if (type.ContainsGenericParameters)
					throw new NotSupportedException(String.Format("Type {0} contains generic parameters", type.FullName));

				var serializer = m_userTypeSerializers.FirstOrDefault(h => h.Handles(type));

				if (serializer == null)
					serializer = s_typeSerializers.FirstOrDefault(h => h.Handles(type));

				if (serializer == null)
					throw new NotSupportedException(String.Format("No serializer for {0}", type.FullName));

				foreach (var t in serializer.GetSubtypes(type))
					stack.Push(t);

				TypeData typeData;

				if (serializer is IStaticTypeSerializer)
				{
					var sts = (IStaticTypeSerializer)serializer;

					MethodInfo writer;
					MethodInfo reader;

					sts.GetStaticMethods(type, out writer, out reader);

					Debug.Assert(writer != null && reader != null);

					typeData = new TypeData(typeID++, writer, reader);

				}
				else if (serializer is IDynamicTypeSerializer)
				{
					var dts = (IDynamicTypeSerializer)serializer;

					typeData = new TypeData(typeID++, dts);
				}
				else
				{
					throw new Exception();
				}

				map[type] = typeData;
			}

			return map;
		}
Example #2
0
        Dictionary <Type, TypeData> GenerateTypeData(IEnumerable <Type> rootTypes)
        {
            var map   = new Dictionary <Type, TypeData>();
            var stack = new Stack <Type>(PrimitivesSerializer.GetSupportedTypes().Concat(rootTypes));

            stack.Push(typeof(object));

            // TypeID 0 is reserved for null
            ushort typeID = 1;

            while (stack.Count > 0)
            {
                var type = stack.Pop();

                if (map.ContainsKey(type))
                {
                    continue;
                }

                if (type.IsAbstract || type.IsInterface)
                {
                    continue;
                }

                if (type.ContainsGenericParameters)
                {
                    throw new NotSupportedException(String.Format("Type {0} contains generic parameters", type.FullName));
                }

                var serializer = m_userTypeSerializers.FirstOrDefault(h => h.Handles(type));

                if (serializer == null)
                {
                    serializer = s_typeSerializers.FirstOrDefault(h => h.Handles(type));
                }

                if (serializer == null)
                {
                    throw new NotSupportedException(String.Format("No serializer for {0}", type.FullName));
                }

                foreach (var t in serializer.GetSubtypes(type))
                {
                    stack.Push(t);
                }

                TypeData typeData;

                if (serializer is IStaticTypeSerializer)
                {
                    var sts = (IStaticTypeSerializer)serializer;

                    MethodInfo writer;
                    MethodInfo reader;

                    sts.GetStaticMethods(type, out writer, out reader);

                    Debug.Assert(writer != null && reader != null);

                    typeData = new TypeData(typeID++, writer, reader);
                }
                else if (serializer is IDynamicTypeSerializer)
                {
                    var dts = (IDynamicTypeSerializer)serializer;

                    typeData = new TypeData(typeID++, dts);
                }
                else
                {
                    throw new Exception();
                }

                map[type] = typeData;
            }

            return(map);
        }