Beispiel #1
0
        internal static TypeDescription GetTypeDescription(Type type, ushort typeId, SerializerState state, bool registerUnderlyingTypes)
        {
            state.LastTypeId = Math.Max((ushort)(typeId + 1), state.LastTypeId);
            if (type.IsBuiltInType())
            {
                var nullableUnderlyingType = Nullable.GetUnderlyingType(type);
                if (nullableUnderlyingType != null)
                {
                    if (!nullableUnderlyingType.IsBuiltInType() && registerUnderlyingTypes)
                    {
                        var td = GetTypeDescription(nullableUnderlyingType, state);
                        td?.Build();
                    }
                }
                return(null);
            }

            TypeDescription description;

            if (state.AllTypeDescriptions.TryGetValue(type, out description))
            {
                description.TypeId = typeId;
                return(description);
            }

            if (type.IsDictionaryType() && registerUnderlyingTypes)
            {
                var dictTypes = type.GetDictionaryArguments();
                var keyTd     = GetTypeDescription(dictTypes.Item1, state);
                keyTd?.Build();
                var valTd = GetTypeDescription(dictTypes.Item2, state);
                valTd?.Build();
                return(null);
            }

            if (type.IsEnumerableType() && registerUnderlyingTypes)
            {
                var elementType = type.GetEnumerableArgument();
                if (!elementType.GetTypeInfo().IsInterface&& !elementType.GetTypeInfo().IsAbstract)
                {
                    var td = GetTypeDescription(elementType, state);
                    td?.Build();
                }
                return(null);
            }

            if (type.IsAnonymousType())
            {
                description = CreateTypeDescription(typeof(AnonymousTypeDescription <>), type, typeId, type, state);
            }
            else
            {
                description = CreateTypeDescription(typeof(ComplexTypeDescription <>), type, typeId, type, state);
            }
            state.AddDescription(type, description);
            return(description);
        }
Beispiel #2
0
        private static void RegisterPrimitiveTypes(SerializerState state)
        {
            var primitiveTypes = BuilderFactory.GetPrimitiveTypes().ToArray();

            for (ushort i = 0; i < primitiveTypes.Length; i++)
            {
                if (state.GetDescription(primitiveTypes[i]) == null)
                {
                    var description = CreateTypeDescription(typeof(SimpleTypeDescription <>), primitiveTypes[i], i, primitiveTypes[i], state);
                    state.AddDescription(primitiveTypes[i], description);
                    description.Build();
                }
            }
        }