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

  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 former MLang Encodings (V1.1)
  18. // We keep the old name now even for the Whidbey V2.0 IObjectReference because it also
  19. // works with the Everett V1.1 version.
  20. namespace System.Text
  21. {
  22.     using System;
  23.     using System.Runtime.Serialization;
  24.     using System.Security.Permissions;
  25.    
  26. /*=================================MLangCodePageEncoding==================================
  27.     ** This class is here only to deserialize the MLang classes from Everett (V1.1) into
  28.     ** Appropriate Whidbey (V2.0) objects.  We also serialize the Whidbey classes
  29.     ** using this proxy since we pretty much need one anyway and that solves Whidbey
  30.     ** to Everett compatibility as well.
  31.     ==============================================================================*/   
  32.    
  33.     [Serializable()]
  34.     internal sealed class MLangCodePageEncoding : ISerializable, IObjectReference
  35.     {
  36.         // Temp stuff
  37.         [NonSerialized()]
  38.         private int m_codePage;
  39.         [NonSerialized()]
  40.         private bool m_isReadOnly;
  41.         [NonSerialized()]
  42.         private bool m_deserializedFromEverett = false;
  43.        
  44.         [NonSerialized()]
  45.         private EncoderFallback encoderFallback = null;
  46.         [NonSerialized()]
  47.         private DecoderFallback decoderFallback = null;
  48.        
  49.         // Might need this when GetRealObjecting
  50.         [NonSerialized()]
  51.         private Encoding realEncoding = null;
  52.        
  53.         // Constructor called by serialization.
  54.         internal MLangCodePageEncoding(SerializationInfo info, StreamingContext context)
  55.         {
  56.             // Any info?
  57.             if (info == null)
  58.                 throw new ArgumentNullException("info");
  59.            
  60.             // All versions have a code page
  61.             this.m_codePage = (int)info.GetValue("m_codePage", typeof(int));
  62.            
  63.             // See if we have a code page
  64.             try {
  65.                 this.m_isReadOnly = (bool)info.GetValue("m_isReadOnly", typeof(bool));
  66.                
  67.                 this.encoderFallback = (EncoderFallback)info.GetValue("encoderFallback", typeof(EncoderFallback));
  68.                 this.decoderFallback = (DecoderFallback)info.GetValue("decoderFallback", typeof(DecoderFallback));
  69.             }
  70.             catch (SerializationException) {
  71.                 this.m_deserializedFromEverett = true;
  72.                
  73.                 // May as well be read only
  74.                 this.m_isReadOnly = true;
  75.             }
  76.         }
  77.        
  78.         // Just get it from GetEncoding
  79.         public object GetRealObject(StreamingContext context)
  80.         {
  81.             this.realEncoding = Encoding.GetEncoding(this.m_codePage);
  82.            
  83.             // If its read only then it uses default fallbacks, otherwise pick up the new ones
  84.             // Otherwise we want to leave the new one read only
  85.             if (!this.m_deserializedFromEverett && !this.m_isReadOnly) {
  86.                 this.realEncoding = (Encoding)this.realEncoding.Clone();
  87.                 this.realEncoding.EncoderFallback = this.encoderFallback;
  88.                 this.realEncoding.DecoderFallback = this.decoderFallback;
  89.             }
  90.            
  91.             return this.realEncoding;
  92.         }
  93.        
  94.         // ISerializable implementation
  95.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  96.         void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  97.         {
  98.             // We cannot ever call this.
  99.             BCLDebug.Assert(false, "Didn't expect to make it to MLangCodePageEncoding ISerializable.GetObjectData");
  100.             throw new ArgumentException(Environment.GetResourceString("Arg_ExecutionEngineException"));
  101.         }
  102.        
  103.         [Serializable()]
  104.         internal sealed class MLangEncoder : ISerializable, IObjectReference
  105.         {
  106.             // Might need this when GetRealObjecting
  107.             [NonSerialized()]
  108.             private Encoding realEncoding = null;
  109.            
  110.             internal MLangEncoder(SerializationInfo info, StreamingContext context)
  111.             {
  112.                 // Any info?
  113.                 if (info == null)
  114.                     throw new ArgumentNullException("info");
  115.                
  116.                 this.realEncoding = (Encoding)info.GetValue("m_encoding", typeof(Encoding));
  117.             }
  118.            
  119.             // Just get it from GetEncoder
  120.             public object GetRealObject(StreamingContext context)
  121.             {
  122.                 return this.realEncoding.GetEncoder();
  123.             }
  124.            
  125.             // ISerializable implementation, get data for this object
  126.             [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  127.             void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  128.             {
  129.                 // We cannot ever call this.
  130.                 BCLDebug.Assert(false, "Didn't expect to make it to MLangCodePageEncoding.MLangEncoder.GetObjectData");
  131.                 throw new ArgumentException(Environment.GetResourceString("Arg_ExecutionEngineException"));
  132.             }
  133.         }
  134.        
  135.        
  136.         [Serializable()]
  137.         internal sealed class MLangDecoder : ISerializable, IObjectReference
  138.         {
  139.             // Might need this when GetRealObjecting
  140.             [NonSerialized()]
  141.             private Encoding realEncoding = null;
  142.            
  143.             internal MLangDecoder(SerializationInfo info, StreamingContext context)
  144.             {
  145.                 // Any info?
  146.                 if (info == null)
  147.                     throw new ArgumentNullException("info");
  148.                
  149.                 this.realEncoding = (Encoding)info.GetValue("m_encoding", typeof(Encoding));
  150.             }
  151.            
  152.             // Just get it from GetDecoder
  153.             public object GetRealObject(StreamingContext context)
  154.             {
  155.                 return this.realEncoding.GetDecoder();
  156.             }
  157.            
  158.             // ISerializable implementation, get data for this object
  159.             [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  160.             void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  161.             {
  162.                 // We cannot ever call this.
  163.                 BCLDebug.Assert(false, "Didn't expect to make it to MLangCodePageEncoding.MLangDecoder.GetObjectData");
  164.                 throw new ArgumentException(Environment.GetResourceString("Arg_ExecutionEngineException"));
  165.             }
  166.         }
  167.     }
  168. }

Developer Fusion