Exemple #1
0
        private void SerializeObject <T>(ref T obj, uint flags)
        {
            if (obj == null)
            {
                if (!CreateObject(out var newObj))
                {
                    obj = (T)newObj;
                    return;
                }

                obj = (T)newObj;
            }

            if (obj != null)
            {
                Type type = obj.GetType();
                if (type == typeof(T))
                {
                    FormatterCache <T> .Instance.Serialize(this, ref obj, flags);
                }
                else
                {
                    FormatterCache.Get(type).Serialize(this, ref Unsafe.As <T, object>(ref obj), flags);
                }
            }
            else
            {
                FormatterCache <T> .Instance.Serialize(this, ref obj, flags);
            }
        }
Exemple #2
0
 public static void EnsureDictionary <TKey, TValue>()
 {
     Ensure(() =>
     {
         var a = new Dictionary <TKey, TValue>();
         FormatterCache <Dictionary <TKey, TValue> > .Register(new DictionaryFormatter <TKey, TValue>());
     });
 }
Exemple #3
0
 public static void EnsureList <T>() where T : new()
 {
     Ensure(() =>
     {
         var a = new List <T>();
         FormatterCache <T> .Register(new ObjectFormatter <T>());
         FormatterCache <T[]> .Register(new ArrayFormatter <T>());
         FormatterCache <List <T> > .Register(new ListFormatter <T>());
     });
 }
Exemple #4
0
        public virtual void Serialize <T>(ref T val, uint flags)
        {
            Type type = typeof(T);

            if (type.IsPrimitive)
            {
                SerializePrimitive(ref val);
            }
            else
            {
                if (val == null)
                {
                    SerializeNull();
                }
                else if (type == val.GetType())
                {
                    FormatterCache <T> .Instance.Serialize(this, ref val, flags);
                }
                else
                {
                    FormatterCache.Get(val.GetType()).Serialize(this, ref Unsafe.As <T, object>(ref val), flags);
                }
            }
        }