Exemplo n.º 1
0
 /// <summary>
 /// Registra um tipo substituto.
 /// </summary>
 /// <param name="surrogate">Implementação do substituto.</param>
 /// <param name="typehandle">Id do tipo.</param>
 /// <param name="cacheContext">Contexto do cache.</param>
 /// <param name="subTypeHandle">Id do subtipo.</param>
 /// <param name="portable">Identifica se é portável.</param>
 /// <returns></returns>
 public static bool RegisterTypeSurrogate(ISerializationSurrogate surrogate, short typehandle, string cacheContext, short subTypeHandle, bool portable)
 {
     surrogate.Require("surrogate").NotNull();
     lock (typeSurrogateMap.SyncRoot)
     {
         if (cacheContext != null)
         {
             Hashtable hashtable = (Hashtable)userTypeHandleSurrogateMap[cacheContext];
             if (hashtable == null)
             {
                 hashtable = new Hashtable();
                 userTypeHandleSurrogateMap.Add(cacheContext, hashtable);
             }
             if (portable)
             {
                 if (hashtable.Contains(typehandle) && ((Hashtable)hashtable[typehandle]).Contains(subTypeHandle))
                 {
                     throw new ArgumentException(ResourceMessageFormatter.Create(() => Properties.Resources.Argument_SubtypeAlreadyRegistered).Format());
                 }
             }
             else if (hashtable.Contains(typehandle))
             {
                 throw new ArgumentException(ResourceMessageFormatter.Create(() => Properties.Resources.Argument_TypeAlreadyRegistered).Format());
             }
             Hashtable hashtable2 = (Hashtable)userTypeSurrogateMap[cacheContext];
             if (hashtable2 == null)
             {
                 hashtable2 = new Hashtable();
                 userTypeSurrogateMap.Add(cacheContext, hashtable2);
             }
             if (!hashtable2.Contains(surrogate.ActualType))
             {
                 if (portable)
                 {
                     surrogate.TypeHandle    = typehandle;
                     surrogate.SubTypeHandle = subTypeHandle;
                     if (!hashtable.Contains(typehandle) || (hashtable[typehandle] == null))
                     {
                         hashtable[typehandle] = new Hashtable();
                     }
                     ((Hashtable)hashtable[typehandle]).Add(subTypeHandle, surrogate);
                     hashtable2.Add(surrogate.ActualType, surrogate);
                     return(true);
                 }
                 surrogate.TypeHandle = typehandle;
                 hashtable2.Add(surrogate.ActualType, surrogate);
                 hashtable.Add(surrogate.TypeHandle, surrogate);
                 return(true);
             }
         }
     }
     return(false);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Registra o substituto no sistema.
 /// </summary>
 /// <param name="surrogate">Instancia do substituto que será registrado.</param>
 /// <param name="typehandle">Id do tipo.</param>
 /// <returns>rue ser o registro foi feito com sucesso.</returns>
 public static bool RegisterTypeSurrogate(ISerializationSurrogate surrogate, short typehandle)
 {
     surrogate.Require("surrogate").NotNull();
     lock (typeSurrogateMap.SyncRoot)
     {
         if (handleSurrogateMap.Contains(typehandle))
         {
             throw new ArgumentException(ResourceMessageFormatter.Create(() => Properties.Resources.Argument_TypeAlreadyRegistered).Format());
         }
         if (!typeSurrogateMap.Contains(surrogate.ActualType))
         {
             surrogate.TypeHandle = typehandle;
             typeSurrogateMap.Add(surrogate.ActualType, surrogate);
             handleSurrogateMap.Add(surrogate.TypeHandle, surrogate);
             return(true);
         }
     }
     return(false);
 }