wuyong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
格式化程序是一个知道如何将任何对象写到一个流中的对象。IFormatter
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;

IFormatter formatter 
= new SoapFormatter();
完整代码
    [SerializableAttribute]
    
public class TestClass:IDeserializationCallback,ISerializable
    
{
        [NonSerializedAttribute]
        
public string aa;     //不可序列化的字段
        public string mystring;
        
string s = "Wahoo!";
        
int n = 6;
        ArrayList oldStrings
=new ArrayList();
        
static string currentVersion="2.0";
        
public TestClass(SerializationInfo info, StreamingContext context)
        
{
            
//mystring = info.GetString("MyString");
            mystring = (string)info.GetValue("MyString"typeof(string));
            
string streamVersion=info.GetString("Version");
            
switch(streamVersion)
            
{
                
case "1.0":
                    s
=info.GetString("MyString");
                    n
=s.Length;
                    
break;
                
case "2.0":
                    s
=info.GetString("MyString");
                    n
=s.Length;
                    oldStrings
=(ArrayList)info.GetValue("OldStrings",typeof(ArrayList));
                    
break;
                
default:
                    
string message=string.Format("Version{0} is not supported.",streamVersion);
                    
throw new SerializationException(message);
                    
            }

        }

        
IDeserializationCallback Members

        
ISerializable Members
    }
posted on 2006-12-31 16:35  吴勇  阅读(201)  评论(0编辑  收藏  举报