示例#1
0
        static Dictionary <Type, TypeData> GenerateTypeData(Type[] types)
        {
            var map = new Dictionary <Type, TypeData>(types.Length);

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

            foreach (var type in types)
            {
                var writer = Primitives.GetWritePrimitive(type);
                var reader = Primitives.GetReadPrimitive(type);

                if ((writer != null) != (reader != null))
                {
                    throw new InvalidOperationException(String.Format("Missing a read or write primitive for {0}", type.FullName));
                }

                var isStatic = writer != null;

                if (type.IsPrimitive && isStatic == false)
                {
                    throw new InvalidOperationException(String.Format("Missing primitive read/write methods for {0}", type.FullName));
                }

                var td = new TypeData(typeID++);

                if (isStatic)
                {
                    td.WriterMethodInfo = writer;
                    td.ReaderMethodInfo = reader;
                    td.IsDynamic        = false;
                }
                else
                {
                    if (typeof(System.Runtime.Serialization.ISerializable).IsAssignableFrom(type))
                    {
                        throw new InvalidOperationException(String.Format("Cannot serialize {0}: ISerializable not supported", type.FullName));
                    }

                    td.IsDynamic = true;
                }

                map[type] = td;
            }

            return(map);
        }