示例#1
0
        /// <summary>
        /// Registers a type that implements <see cref="ICompactSerializable"/> with the system. If the
        /// type is an array of <see cref="ICompactSerializable"/>s appropriate surrogates for arrays
        /// and the element type are also registered.
        /// </summary>
        /// <param name="type">type that implements <see cref="ICompactSerializable"/></param>
        /// <exception cref="ArgumentNullException">If <param name="type"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If the <param name="type"/> is already registered or when no appropriate surrogate
        /// is found for the specified <param name="type"/>.
        /// </exception>
        static public void RegisterCompactType(Type type, IObjectPool pool = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (TypeSurrogateSelector.GetSurrogateForTypeStrict(type, null) != null)
            {
                throw new ArgumentException("Type '" + type.FullName + "' is already registered");
            }

            ISerializationSurrogate surrogate = null;

            //if (typeof(IDictionary).IsAssignableFrom(type))
            //{
            //    surrogate = new IDictionarySerializationSurrogate(type);
            //}
            if (typeof(Dictionary <,>).Equals(type))
            {
                surrogate = new IDictionarySerializationSurrogate(type);
            }
            else if (type.IsArray)
            {
                surrogate = new ArraySerializationSurrogate(type);
            }
            //else if (typeof(IList).IsAssignableFrom(type))
            //{
            //    surrogate = new IListSerializationSurrogate(type);
            //}
            else if (typeof(List <>).Equals(type))
            {
                surrogate = new IListSerializationSurrogate(type);
            }
            else if (typeof(ICompactSerializable).IsAssignableFrom(type))
            {
                surrogate = new ICompactSerializableSerializationSurrogate(type, null);
            }
            else if (typeof(Enum).IsAssignableFrom(type))
            {
                surrogate = new EnumSerializationSurrogate(type);
            }

            if (surrogate == null)
            {
                throw new ArgumentException("No appropriate surrogate found for type " + type.FullName);
            }

            TypeSurrogateSelector.RegisterTypeSurrogate(surrogate);
        }
示例#2
0
        static private void RegisterCompactType(Type type, short typeHandle, bool versionCompatible, IObjectPool pool = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            ISerializationSurrogate surrogate = null;

            if ((surrogate = TypeSurrogateSelector.GetSurrogateForTypeStrict(type, null)) != null)
            {
                if (surrogate.ObjectPool == null && pool != null)
                {
                    surrogate.ObjectPool = pool;
                }

                //No need to check subHandle since this funciton us not used by DataSharing
                if (surrogate.TypeHandle == typeHandle)
                {
                    return; //Type is already registered with same handle.
                }
                throw new ArgumentException("Type " + type.FullName + " is already registered with different handle");
            }

            //if (typeof(IDictionary).IsAssignableFrom(type))
            //{
            //    if (type.IsGenericType)
            //        surrogate = new GenericIDictionarySerializationSurrogate(typeof(IDictionary<,>));
            //    else
            //        surrogate = new IDictionarySerializationSurrogate(type);
            //}
            if (typeof(Dictionary <,>).Equals(type))
            {
                if (type.IsGenericType)
                {
                    surrogate = new GenericIDictionarySerializationSurrogate(typeof(IDictionary <,>));
                }
                else
                {
                    surrogate = new IDictionarySerializationSurrogate(type);
                }
            }
            else if (type.IsArray)
            {
                surrogate = new ArraySerializationSurrogate(type);
            }
            //else if (typeof(IList).IsAssignableFrom(type))
            //{
            //    if (type.IsGenericType)
            //        surrogate = new GenericIListSerializationSurrogate(typeof(IList<>));
            //    else
            //        surrogate = new IListSerializationSurrogate(type);
            //}
            else if (typeof(List <>).Equals(type))
            {
                if (type.IsGenericType)
                {
                    surrogate = new GenericIListSerializationSurrogate(typeof(IList <>));
                }
                else
                {
                    surrogate = new IListSerializationSurrogate(type);
                }
            }
            else if (typeof(ICompactSerializable).IsAssignableFrom(type))
            {
                if (versionCompatible)
                {
                    surrogate = new VersionCompatibleCompactSerializationSurrogate(type, pool);
                }
                else
                {
                    surrogate = new ICompactSerializableSerializationSurrogate(type, pool);
                }
            }
            else if (typeof(Enum).IsAssignableFrom(type))
            {
                surrogate = new EnumSerializationSurrogate(type);
            }

            if (surrogate == null)
            {
                throw new ArgumentException("No appropriate surrogate found for type " + type.FullName);
            }

            TypeSurrogateSelector.RegisterTypeSurrogate(surrogate, typeHandle);
        }
示例#3
0
        /// <summary>
        /// Registers a type that implements <see cref="ICompactSerializable"/> with the system. If the
        /// type is an array of <see cref="ICompactSerializable"/>s appropriate surrogates for arrays
        /// and the element type are also registered.
        /// </summary>
        /// <param name="type">type that implements <see cref="ICompactSerializable"/></param>
        /// <exception cref="ArgumentNullException">If <param name="type"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If the <param name="type"/> is already registered or when no appropriate surrogate
        /// is found for the specified <param name="type"/>.
        /// </exception>
        static public void RegisterCustomCompactType(Type type, short typeHandle, string cacheContext, short subTypeHandle, Hashtable attributeOrder, bool portable, Hashtable nonCompactFields, IObjectPool pool = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            ISerializationSurrogate surrogate = null;

            if (cacheContext == null)
            {
                throw new ArgumentException("cacheContext can not be null");
            }

            if ((surrogate = TypeSurrogateSelector.GetSurrogateForTypeStrict(type, cacheContext)) != null)
            {
                if (surrogate.ObjectPool == null && pool != null)
                {
                    surrogate.ObjectPool = pool;
                }

                if (surrogate.TypeHandle == typeHandle && (surrogate.SubTypeHandle == subTypeHandle || surrogate.SubTypeHandle != 0))
                {
                    return;                     //Type is already registered with same handle.
                }
                throw new ArgumentException("Type " + type.FullName + " is already registered with different handle");
            }

            if (typeof(Dictionary <,>).Equals(type) && string.IsNullOrEmpty(((Type[])type.GetGenericArguments())[0].FullName))
            {
                if (type.IsGenericType)
                {
                    surrogate = new GenericIDictionarySerializationSurrogate(typeof(IDictionary <,>));
                }
                else
                {
                    surrogate = new IDictionarySerializationSurrogate(type);
                }
            }
            else if (type.IsArray)

            {
                surrogate = new ArraySerializationSurrogate(type);
            }

            else if (typeof(List <>).Equals(type) && string.IsNullOrEmpty(((Type[])type.GetGenericArguments())[0].FullName))
            {
                if (type.IsGenericType)
                {
                    surrogate = new GenericIListSerializationSurrogate(typeof(IList <>));
                }
                else
                {
                    surrogate = new IListSerializationSurrogate(type);
                }
            }
            else if (typeof(ICompactSerializable).IsAssignableFrom(type))
            {
                surrogate = new ICompactSerializableSerializationSurrogate(type, pool);
            }
            else if (typeof(Enum).IsAssignableFrom(type))
            {
                surrogate = new EnumSerializationSurrogate(type);
            }
            else
            {
                lock (mutex)
                {
                    DynamicSurrogateBuilder.Portable = portable;
                    if (portable)
                    {
                        DynamicSurrogateBuilder.SubTypeHandle = subTypeHandle;
                    }
                    surrogate = DynamicSurrogateBuilder.CreateTypeSurrogate(type, attributeOrder, nonCompactFields);
                }
            }

            if (surrogate == null)
            {
                throw new ArgumentException("No appropriate surrogate found for type " + type.FullName);
            }

            TypeSurrogateSelector.RegisterTypeSurrogate(surrogate, typeHandle, cacheContext, subTypeHandle, portable);
        }