The Labs \ Source Viewer \ SSCLI \ System.Text \ CodePageEncoding

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. // WARNING:
  16. //
  17. // This is just an IObjectReference proxy for the Code Page Encodings.
  18. namespace System.Text
  19. {
  20.     using System;
  21.     using System.Runtime.Serialization;
  22.     using System.Security.Permissions;
  23.    
  24. /*=================================CodePageEncoding==================================
  25.     ** This class is here only to deserialize the Code Page classes from Everett (V1.1) into
  26.     ** Appropriate Whidbey (V2.0) objects.  We also serialize the Whidbey classes
  27.     ** using this proxy since we pretty much need one anyway and that solves Whidbey
  28.     ** to Everett compatibility as well.
  29.     ==============================================================================*/   
  30.    
  31.     [Serializable()]
  32.     internal sealed class CodePageEncoding : ISerializable, IObjectReference
  33.     {
  34.         // Temp stuff
  35.         [NonSerialized()]
  36.         private int m_codePage;
  37.         [NonSerialized()]
  38.         private bool m_isReadOnly;
  39.         [NonSerialized()]
  40.         private bool m_deserializedFromEverett = false;
  41.        
  42.         [NonSerialized()]
  43.         private EncoderFallback encoderFallback = null;
  44.         [NonSerialized()]
  45.         private DecoderFallback decoderFallback = null;
  46.        
  47.         // Might need this when GetRealObjecting
  48.         [NonSerialized()]
  49.         private Encoding realEncoding = null;
  50.        
  51.         // Constructor called by serialization.
  52.         internal CodePageEncoding(SerializationInfo info, StreamingContext context)
  53.         {
  54.             // Any info?
  55.             if (info == null)
  56.                 throw new ArgumentNullException("info");
  57.            
  58.             // All versions have a code page
  59.             this.m_codePage = (int)info.GetValue("m_codePage", typeof(int));
  60.            
  61.             // See if we have a code page
  62.             try {
  63.                 this.m_isReadOnly = (bool)info.GetValue("m_isReadOnly", typeof(bool));
  64.                
  65.                 this.encoderFallback = (EncoderFallback)info.GetValue("encoderFallback", typeof(EncoderFallback));
  66.                 this.decoderFallback = (DecoderFallback)info.GetValue("decoderFallback", typeof(DecoderFallback));
  67.             }
  68.             catch (SerializationException) {
  69.                 this.m_deserializedFromEverett = true;
  70.                
  71.                 // May as well be read only
  72.                 this.m_isReadOnly = true;
  73.             }
  74.         }
  75.        
  76.         // Just get it from GetEncoding
  77.         public object GetRealObject(StreamingContext context)
  78.         {
  79.             this.realEncoding = Encoding.GetEncoding(this.m_codePage);
  80.            
  81.             // If its read only then it uses default fallbacks, otherwise pick up the new ones
  82.             // Otherwise we want to leave the new one read only
  83.             if (!this.m_deserializedFromEverett && !this.m_isReadOnly) {
  84.                 this.realEncoding = (Encoding)this.realEncoding.Clone();
  85.                 this.realEncoding.EncoderFallback = this.encoderFallback;
  86.                 this.realEncoding.DecoderFallback = this.decoderFallback;
  87.             }
  88.            
  89.             return this.realEncoding;
  90.         }
  91.        
  92.         // ISerializable implementation
  93.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  94.         void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  95.         {
  96.             // We cannot ever call this.
  97.             BCLDebug.Assert(false, "Didn't expect to make it to CodePageEncoding ISerializable.GetObjectData");
  98.             throw new ArgumentException(Environment.GetResourceString("Arg_ExecutionEngineException"));
  99.         }
  100.        
  101.         [Serializable()]
  102.         internal sealed class Decoder : ISerializable, IObjectReference
  103.         {
  104.             // Might need this when GetRealObjecting
  105.             [NonSerialized()]
  106.             private Encoding realEncoding = null;
  107.            
  108.             internal Decoder(SerializationInfo info, StreamingContext context)
  109.             {
  110.                 // Any info?
  111.                 if (info == null)
  112.                     throw new ArgumentNullException("info");
  113.                
  114.                 this.realEncoding = (Encoding)info.GetValue("encoding", typeof(Encoding));
  115.             }
  116.            
  117.             // Just get it from GetDecider
  118.             public object GetRealObject(StreamingContext context)
  119.             {
  120.                 return this.realEncoding.GetDecoder();
  121.             }
  122.            
  123.             // ISerializable implementation, get data for this object
  124.             [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  125.             void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  126.             {
  127.                 // We cannot ever call this.
  128.                 BCLDebug.Assert(false, "Didn't expect to make it to CodePageEncoding.Decoder.GetObjectData");
  129.                 throw new ArgumentException(Environment.GetResourceString("Arg_ExecutionEngineException"));
  130.             }
  131.         }
  132.     }
  133. }

Developer Fusion