⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 diskfullexception.cs

📁 Microsoft.NET.框架程序设计修订版中的书中源码
💻 CS
字号:
/******************************************************************************
Module:  DiskFullException.cs
Notices: Copyright (c) 2002 Jeffrey Richter
******************************************************************************/


using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;


///////////////////////////////////////////////////////////////////////////////


// Allow instances of DiskFullException to be serialized
[Serializable]
sealed class DiskFullException : Exception, ISerializable {
   // The three public constructors
   public DiskFullException() 
      : base() {  // Call base constructor
   }

   public DiskFullException(String message) 
      : base(message) { // Call base constructor
   }

   public DiskFullException(String message, Exception innerException) 
      : base(message, innerException) {   // Call base constructor
   }


   // Define a private field
   private String diskpath;

   // Define a virtual read-only property that returns the field
   public String DiskPath { get { return diskpath; } }


   // Override the public Message property so that our 
   // field is included in the message
   public override String Message {
      get {
         String msg = base.Message;
         if (diskpath != null) 
            msg += Environment.NewLine + "Disk Path: " + diskpath;
         return msg;
      }
   }


   // Because at least 1 field is defined, 
   // define the special deserialization constructor
   // Since this class is sealed, this constructor is private
   // If this class were not sealed, this constructor should be protected
   private DiskFullException(SerializationInfo info, 
      StreamingContext context) 
      : base(info, context) { // Let the base deserialize its fields

      // Deserialize each field
      diskpath = info.GetString("DiskPath");
   }

   // Because at least 1 field is defined, 
   // define the serialization method
   void ISerializable.GetObjectData(SerializationInfo info,
      StreamingContext context) {
      // Serialize each field
      info.AddValue("DiskPath", diskpath);

      // Let the base type serialize its fields
      base.GetObjectData(info, context);
   }


   // Define additional constructors that set the field
   public DiskFullException(String message, String diskpath) 
      : this(message) { // Call our constructor
      this.diskpath = diskpath;
   }

   public DiskFullException(String message, String diskpath, 
      Exception innerException) 
      : this(message, innerException) {   // Call our constructor
      this.diskpath = diskpath;
   }
}


///////////////////////////////////////////////////////////////////////////////


// The code below tests the serialization of the exception
class App {
   static void Main() {
      // Construct a DiskFullException object
      DiskFullException e = 
         new DiskFullException("The disk volume is full", "C:\\");

      // Serialize the DiskFullException ebject
      FileStream fs = new FileStream("Test", FileMode.Create);
      IFormatter f = new SoapFormatter();
      f.Serialize(fs, e);
      fs.Close();

      // Deserialize the DiskFullException object and check it's fields
      fs = new FileStream("Test", FileMode.Open);
      e = (DiskFullException) f.Deserialize(fs);
      fs.Close();
      Console.WriteLine("Type: {1}{0}DiskPath: {2}{0}Message: {3}",
         Environment.NewLine, e.GetType(), e.DiskPath, e.Message);
   }
}


//////////////////////////////// End of File //////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -