示例#1
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);
            }
        }
示例#2
0
 /// <summary>
 /// Remove o registro de todos os tipos compactor customizados.
 /// </summary>
 /// <param name="cacheContext"></param>
 public static void UnregisterAllCustomCompactTypes(string cacheContext)
 {
     cacheContext.Require("cacheContext").NotNull();
     TypeSurrogateSelector.UnregisterAllSurrogates(cacheContext);
 }
示例#3
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);
            }
        }