Ejemplo n.º 1
0
        /// <summary>
        /// Registra um tipo compacto.
        /// </summary>
        /// <param name="type"></param>
        public static void RegisterCompactType(Type type)
        {
            type.Require("type").NotNull();
            if (TypeSurrogateSelector.GetSurrogateForTypeStrict(type, null) != null)
            {
                throw new ArgumentException(ResourceMessageFormatter.Create(() => Properties.Resources.Argument_TypeAlreadyRegistered2, type.FullName).Format());
            }
            ISerializationSurrogate surrogate = null;

            if (typeof(IDictionary).IsAssignableFrom(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(ICompactSerializable).IsAssignableFrom(type))
            {
                surrogate = new ICompactSerializableSerializationSurrogate(type);
            }
            else if (typeof(Enum).IsAssignableFrom(type))
            {
                surrogate = new EnumSerializationSurrogate(type);
            }
            if (surrogate == null)
            {
                throw new ArgumentException(ResourceMessageFormatter.Create(() => Properties.Resources.Argument_NoAppropriateSurrogateFound, type.FullName).Format());
            }
            TypeSurrogateSelector.RegisterTypeSurrogate(surrogate);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Registra o tipo compacto.
        /// </summary>
        /// <param name="type">Tipo que será registrado.</param>
        /// <param name="typeHandle">Número do manipulador do tipo.</param>
        public static void RegisterCompactType(Type type, short typeHandle)
        {
            type.Require("type").NotNull();
            ISerializationSurrogate surrogateForTypeStrict = null;

            surrogateForTypeStrict = TypeSurrogateSelector.GetSurrogateForTypeStrict(type, null);
            if (surrogateForTypeStrict != null)
            {
                if (surrogateForTypeStrict.TypeHandle != typeHandle)
                {
                    throw new ArgumentException(ResourceMessageFormatter.Create(() => Properties.Resources.Argument_TypeAlreadyRegisteredWithDifferentHandle, type.FullName).Format());
                }
            }
            else
            {
                if (typeof(IDictionary).IsAssignableFrom(type))
                {
                    if (type.IsGenericType)
                    {
                        surrogateForTypeStrict = new GenericIDictionarySerializationSurrogate(typeof(IDictionary <, >));
                    }
                    else
                    {
                        surrogateForTypeStrict = new IDictionarySerializationSurrogate(type);
                    }
                }
                else if (type.IsArray)
                {
                    surrogateForTypeStrict = new ArraySerializationSurrogate(type);
                }
                else if (typeof(IList).IsAssignableFrom(type))
                {
                    if (type.IsGenericType)
                    {
                        surrogateForTypeStrict = new GenericIListSerializationSurrogate(typeof(IList <>));
                    }
                    else
                    {
                        surrogateForTypeStrict = new IListSerializationSurrogate(type);
                    }
                }
                else if (typeof(ICompactSerializable).IsAssignableFrom(type))
                {
                    surrogateForTypeStrict = new ICompactSerializableSerializationSurrogate(type);
                }
                else if (typeof(Enum).IsAssignableFrom(type))
                {
                    surrogateForTypeStrict = new EnumSerializationSurrogate(type);
                }
                if (surrogateForTypeStrict == null)
                {
                    throw new ArgumentException(ResourceMessageFormatter.Create(() => Properties.Resources.Argument_NoAppropriateSurrogateFoundForType, type.FullName).Format());
                }
                TypeSurrogateSelector.RegisterTypeSurrogate(surrogateForTypeStrict, typeHandle);
            }
        }
Ejemplo n.º 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 RegisterCompactType(Type type)
        {
            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);
            }
            else if (typeof(Enum).IsAssignableFrom(type))
            {
                surrogate = new EnumSerializationSurrogate(type);
            }

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

            System.Diagnostics.Debug.WriteLine("Registered suurogate for type " + type.FullName);
            TypeSurrogateSelector.RegisterTypeSurrogate(surrogate);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Registra um tipo customizado compacto.
        /// </summary>
        /// <param name="type">Tipo que será registrado.</param>
        /// <param name="typeHandle"></param>
        /// <param name="cacheContext"></param>
        /// <param name="subTypeHandle"></param>
        /// <param name="attributeOrder"></param>
        /// <param name="portable"></param>
        public static void RegisterCustomCompactType(Type type, short typeHandle, string cacheContext, short subTypeHandle, Hashtable attributeOrder, bool portable)
        {
            type.Require("type").NotNull();
            cacheContext.Require("cacheContext").NotNull();
            var surrogateForTypeStrict = TypeSurrogateSelector.GetSurrogateForTypeStrict(type, cacheContext);

            if (surrogateForTypeStrict != null)
            {
                if ((surrogateForTypeStrict.TypeHandle != typeHandle) || ((surrogateForTypeStrict.SubTypeHandle != subTypeHandle) && (surrogateForTypeStrict.SubTypeHandle == 0)))
                {
                    throw new ArgumentException(ResourceMessageFormatter.Create(() => Properties.Resources.Argument_TypeAlreadyRegisteredWithDifferentHandle, type.FullName).Format());
                }
            }
            else
            {
                if (typeof(IDictionary).IsAssignableFrom(type))
                {
                    if (type.IsGenericType)
                    {
                        surrogateForTypeStrict = new GenericIDictionarySerializationSurrogate(typeof(IDictionary <, >));
                    }
                    else
                    {
                        surrogateForTypeStrict = new IDictionarySerializationSurrogate(type);
                    }
                }
                else if (type.IsArray)
                {
                    surrogateForTypeStrict = new ArraySerializationSurrogate(type);
                }
                else if (typeof(IList).IsAssignableFrom(type))
                {
                    if (type.IsGenericType)
                    {
                        surrogateForTypeStrict = new GenericIListSerializationSurrogate(typeof(IList <>));
                    }
                    else
                    {
                        surrogateForTypeStrict = new IListSerializationSurrogate(type);
                    }
                }
                else if (typeof(ICompactSerializable).IsAssignableFrom(type))
                {
                    surrogateForTypeStrict = new ICompactSerializableSerializationSurrogate(type);
                }
                else if (typeof(Enum).IsAssignableFrom(type))
                {
                    surrogateForTypeStrict = new EnumSerializationSurrogate(type);
                }
                else
                {
                    DynamicSurrogateBuilder.Portable = portable;
                    if (portable)
                    {
                        DynamicSurrogateBuilder.SubTypeHandle = subTypeHandle;
                    }
                    surrogateForTypeStrict = DynamicSurrogateBuilder.CreateTypeSurrogate(type, attributeOrder);
                }
                if (surrogateForTypeStrict == null)
                {
                    throw new ArgumentException("No appropriate surrogate found for type " + type.FullName);
                }
                TypeSurrogateSelector.RegisterTypeSurrogate(surrogateForTypeStrict, typeHandle, cacheContext, subTypeHandle, portable);
            }
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
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);
        }