/**
         * Process a single object to be pickled.
         */
        private bool dispatch(Type t, object o)
        {
            Debug.Assert(t != null);
            Debug.Assert(o != null);
            Debug.Assert(t == o.GetType());

            // is it a primitive array?
            if (o is Array)
            {
                Type componentType = t.GetElementType();
                if (componentType != null && componentType.IsPrimitive)
                {
                    put_arrayOfPrimitives(componentType, o);
                }
                else if (o is string[] strings)
                {
                    put_arrayOfStrings(strings);
                }
                else
                {
                    put_arrayOfObjects((object[])o);
                }
                return(true);
            }

            if (t.IsPrimitive)
            {
                switch (o)
                {
                case double v:
                    put_float(v);
                    return(true);

                case int v:
                    put_long(v);
                    return(true);

                case bool v:
                    put_bool(v);
                    return(true);

                case float v:
                    put_float(v);
                    return(true);

                case long v:
                    put_long(v);
                    return(true);

                case byte v:
                    put_byte(v);
                    return(true);

                case sbyte v:
                    put_long(v);
                    return(true);

                case short v:
                    put_long(v);
                    return(true);

                case ushort v:
                    put_long(v);
                    return(true);

                case uint v:
                    put_long(v);
                    return(true);

                case ulong v:
                    put_ulong(v);
                    return(true);

                case char v:
                    put_string(v.ToString());
                    return(true);
                }
            }
            else
            {
                switch (o)
                {
                case string v:
                    put_string(v);
                    return(true);

                case Enum v:
                    put_string(o.ToString());
                    return(true);

                case decimal v:
                    put_decimal(v);
                    return(true);

                case DateTime v:
                    put_datetime(v);
                    return(true);

                case TimeSpan v:
                    put_timespan(v);
                    return(true);

                case IDictionary v:
                    put_map(v);
                    return(true);

                case IList v:
                    put_enumerable(v);
                    return(true);

                case IEnumerable v:
                    if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(HashSet <>))
                    {
                        put_set(v);
                    }
                    else
                    {
                        put_enumerable(v);
                    }
                    return(true);
                }
            }

            // check registry
            IObjectPickler custompickler = pickler.getCustomPickler(t);

            if (custompickler != null)
            {
                // to support this scenario this type derives from Stream and implements Write methods
                custompickler.pickle(o, this, pickler);
                WriteMemo(o);
                return(true);
            }

            // more complex types
            DataContractAttribute dca = (DataContractAttribute)Attribute.GetCustomAttribute(t, typeof(DataContractAttribute));

            if (dca != null)
            {
                put_datacontract(t, o, dca);
                return(true);
            }

            SerializableAttribute sa = (SerializableAttribute)Attribute.GetCustomAttribute(t, typeof(SerializableAttribute));

            if (sa != null)
            {
                put_serializable(t, o);
                return(true);
            }

            if (hasPublicProperties(t))
            {
                put_objwithproperties(o);
                return(true);
            }

            return(false);
        }
Beispiel #2
0
 /**
  * Register additional object picklers for custom classes.
  */
 public static void registerCustomPickler(Type clazz, IObjectPickler pickler)
 {
     customPicklers[clazz] = pickler;
 }
Beispiel #3
0
        /**
         * Process a single object to be pickled.
         */
        private bool dispatch(Type t, object o, out bool must_memo)
        {
            // is it a primitive array?
            must_memo = true;

            if (o is Array)
            {
                Type componentType = t.GetElementType();
                if (componentType.IsPrimitive)
                {
                    put_arrayOfPrimitives(componentType, o);
                }
                else
                {
                    put_arrayOfObjects((object[])o);
                }
                return(true);
            }

            // first the primitive types
            if (o is bool)
            {
                put_bool((Boolean)o);
                must_memo = false;
                return(true);
            }
            if (o is byte)
            {
                put_long((byte)o);
                must_memo = false;
                return(true);
            }
            if (o is sbyte)
            {
                put_long((sbyte)o);
                must_memo = false;
                return(true);
            }
            if (o is short)
            {
                put_long((short)o);
                must_memo = false;
                return(true);
            }
            if (o is ushort)
            {
                put_long((ushort)o);
                must_memo = false;
                return(true);
            }
            if (o is int)
            {
                put_long((int)o);
                must_memo = false;
                return(true);
            }
            if (o is uint)
            {
                put_long((uint)o);
                must_memo = false;
                return(true);
            }
            if (o is long)
            {
                put_long((long)o);
                must_memo = false;
                return(true);
            }
            if (o is ulong)
            {
                put_ulong((ulong)o);
                must_memo = false;
                return(true);
            }
            if (o is float)
            {
                put_float((float)o);
                must_memo = false;
                return(true);
            }
            if (o is double)
            {
                put_float((double)o);
                must_memo = false;
                return(true);
            }
            if (o is char)
            {
                put_string("" + o);
                must_memo = false;
                return(true);
            }

            // check registry
            if (customPicklers.ContainsKey(t))
            {
                IObjectPickler custompickler = customPicklers[t];
                custompickler.pickle(o, this.outs, this);
                return(true);
            }

            // more complex types
            if (o is string)
            {
                put_string((String)o);
                return(true);
            }
            if (o is decimal)
            {
                put_decimal((decimal)o);
                return(true);
            }
            if (o is DateTime)
            {
                put_datetime((DateTime)o);
                return(true);
            }
            if (o is TimeSpan)
            {
                put_timespan((TimeSpan)o);
                return(true);
            }
            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(HashSet <>))
            {
                put_set((IEnumerable)o);
                return(true);
            }
            if (o is IDictionary)
            {
                put_map((IDictionary)o);
                return(true);
            }
            if (o is IList)
            {
                put_enumerable((IList)o);
                return(true);
            }
            if (o is IEnumerable)
            {
                put_enumerable((IEnumerable)o);
                return(true);
            }
            if (o is Enum)
            {
                put_string(o.ToString());
                return(true);
            }
            if (hasPublicProperties(o))
            {
                put_objwithproperties(o);
                return(true);
            }
            must_memo = false;
            return(false);
        }
Beispiel #4
0
	/**
	 * Register additional object picklers for custom classes.
	 */
	public static void registerCustomPickler(Type clazz, IObjectPickler pickler) {
		customPicklers[clazz]=pickler;
	}
Beispiel #5
0
        /**
         * Process a single object to be pickled.
         */
        private bool dispatch(Type t, object o)
        {
            // is it a primitive array?
            if (o is Array)
            {
                Type componentType = t.GetElementType();
                if (componentType.IsPrimitive)
                {
                    put_arrayOfPrimitives(componentType, o);
                }
                else
                {
                    put_arrayOfObjects((object[])o);
                }
                return(true);
            }

            // first the primitive types
            if (o is bool)
            {
                put_bool((Boolean)o);
                return(true);
            }
            if (o is byte)
            {
                put_long((byte)o);
                return(true);
            }
            if (o is sbyte)
            {
                put_long((sbyte)o);
                return(true);
            }
            if (o is short)
            {
                put_long((short)o);
                return(true);
            }
            if (o is ushort)
            {
                put_long((ushort)o);
                return(true);
            }
            if (o is int)
            {
                put_long((int)o);
                return(true);
            }
            if (o is uint)
            {
                put_long((uint)o);
                return(true);
            }
            if (o is long)
            {
                put_long((long)o);
                return(true);
            }
            if (o is ulong)
            {
                put_ulong((ulong)o);
                return(true);
            }
            if (o is float)
            {
                put_float((float)o);
                return(true);
            }
            if (o is double)
            {
                put_float((double)o);
                return(true);
            }
            if (o is char)
            {
                put_string("" + o);
                return(true);
            }

            // check registry
            IObjectPickler custompickler = getCustomPickler(t);

            if (custompickler != null)
            {
                custompickler.pickle(o, this.outs, this);
                WriteMemo(o);
                return(true);
            }

            // more complex types
            if (o is string)
            {
                put_string((String)o);
                return(true);
            }
            if (o is decimal)
            {
                put_decimal((decimal)o);
                return(true);
            }
            if (o is DateTime)
            {
                put_datetime((DateTime)o);
                return(true);
            }
            if (o is TimeSpan)
            {
                put_timespan((TimeSpan)o);
                return(true);
            }
            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(HashSet <>))
            {
                put_set((IEnumerable)o);
                return(true);
            }
            if (o is IDictionary)
            {
                put_map((IDictionary)o);
                return(true);
            }
            if (o is IList)
            {
                put_enumerable((IList)o);
                return(true);
            }
            if (o is IEnumerable)
            {
                put_enumerable((IEnumerable)o);
                return(true);
            }
            if (o is Enum)
            {
                put_string(o.ToString());
                return(true);
            }

            DataContractAttribute dca = (DataContractAttribute)Attribute.GetCustomAttribute(t, typeof(DataContractAttribute));

            if (dca != null)
            {
                put_datacontract(t, o, dca);
                return(true);
            }

            SerializableAttribute sa = (SerializableAttribute)Attribute.GetCustomAttribute(t, typeof(SerializableAttribute));

            if (sa != null)
            {
                put_serializable(t, o);
                return(true);
            }

            if (hasPublicProperties(o))
            {
                put_objwithproperties(o);
                return(true);
            }

            return(false);
        }
Beispiel #6
0
        /**
         * Process a single object to be pickled.
         */
        private bool dispatch(Type t, object o)
        {
            Debug.Assert(t != null);
            Debug.Assert(o != null);
            Debug.Assert(t == o.GetType());

            // is it a primitive array?
            if (o is Array)
            {
                Type componentType = t.GetElementType();
                if (componentType != null && componentType.IsPrimitive)
                {
                    put_arrayOfPrimitives(componentType, o);
                }
                else
                {
                    put_arrayOfObjects((object[])o);
                }
                return(true);
            }

            // first check for enums, as GetTypeCode will return the underlying type.
            if (o is Enum)
            {
                put_string(o.ToString());
                return(true);
            }

            // first the primitive types
            switch (Type.GetTypeCode(t))
            {
            case TypeCode.Boolean:
                put_bool((bool)o);
                return(true);

            case TypeCode.Byte:
                put_long((byte)o);
                return(true);

            case TypeCode.SByte:
                put_long((sbyte)o);
                return(true);

            case TypeCode.Int16:
                put_long((short)o);
                return(true);

            case TypeCode.UInt16:
                put_long((ushort)o);
                return(true);

            case TypeCode.Int32:
                put_long((int)o);
                return(true);

            case TypeCode.UInt32:
                put_long((uint)o);
                return(true);

            case TypeCode.Int64:
                put_long((long)o);
                return(true);

            case TypeCode.UInt64:
                put_ulong((ulong)o);
                return(true);

            case TypeCode.Single:
                put_float((float)o);
                return(true);

            case TypeCode.Double:
                put_float((double)o);
                return(true);

            case TypeCode.Char:
                put_string(((char)o).ToString());
                return(true);

            case TypeCode.String:
                put_string((string)o);
                return(true);

            case TypeCode.Decimal:
                put_decimal((decimal)o);
                return(true);

            case TypeCode.DateTime:
                put_datetime((DateTime)o);
                return(true);
            }

            // check registry
            IObjectPickler custompickler = getCustomPickler(t);

            if (custompickler != null)
            {
                custompickler.pickle(o, outs, this);
                WriteMemo(o);
                return(true);
            }

            // more complex types
            if (o is TimeSpan)
            {
                put_timespan((TimeSpan)o);
                return(true);
            }
            if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(HashSet <>))
            {
                put_set((IEnumerable)o);
                return(true);
            }

            var dictionary = o as IDictionary;

            if (dictionary != null)
            {
                put_map(dictionary);
                return(true);
            }

            var list = o as IList;

            if (list != null)
            {
                put_enumerable(list);
                return(true);
            }

            var enumerable = o as IEnumerable;

            if (enumerable != null)
            {
                put_enumerable(enumerable);
                return(true);
            }

            DataContractAttribute dca = (DataContractAttribute)Attribute.GetCustomAttribute(t, typeof(DataContractAttribute));

            if (dca != null)
            {
                put_datacontract(t, o, dca);
                return(true);
            }

            SerializableAttribute sa = (SerializableAttribute)Attribute.GetCustomAttribute(t, typeof(SerializableAttribute));

            if (sa != null)
            {
                put_serializable(t, o);
                return(true);
            }

            if (hasPublicProperties(o))
            {
                put_objwithproperties(o);
                return(true);
            }

            return(false);
        }