Esempio n. 1
0
 public static void Clear()
 {
     lock (Locker)
     {
         var state = new SerializerState();
         Interlocked.Exchange(ref _state, state);
     }
 }
Esempio n. 2
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);
        }
Esempio n. 3
0
 public static void Clear()
 {
     lock (Locker)
     {
         var state = new SerializerState();
         state.DescriptionsById = new SerializerState.TypeDescriptionHolder[1];
         Interlocked.Exchange(ref _state, state);
     }
 }
Esempio n. 4
0
        internal SerializerState Clone()
        {
            var copy = new SerializerState
            {
                AllTypeDescriptions = AllTypeDescriptions.ToDictionary(i => i.Key, i => i.Value),
                LastTypeId          = LastTypeId,
                _descriptions       = _descriptions.Clone()
            };

            return(copy);
        }
Esempio n. 5
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();
                }
            }
        }
Esempio n. 6
0
 internal static TypeDescription GetTypeDescription(Type type, SerializerState state)
 {
     return(GetTypeDescription(type, state.LastTypeId, state, true));
 }