コード例 #1
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);
            }
        }
コード例 #2
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);
        }