| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 344 人关注过本帖
标题:解释代码
取消只看楼主 加入收藏
昨日的忧伤
Rank: 1
来 自:福建三明
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-6-11
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
解释代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Reflection;
namespace ConsoleApplication2
{
    class TestObjectManager
    {
        static void Main()
        {
            SerializationInfo infoClass1 = new SerializationInfo(typeof(SampleClass1), new FormatterConverter());    //使用SerializationInfo实例化一个名为infoClass1的实例
            SampleStructure sampleStruct = new SampleStructure();
            sampleStruct.VarInt = 100;
            infoClass1.AddValue("ObjSampleClass1", sampleStruct);
            SerializationInfo infoClass2 = new SerializationInfo(typeof(SampleClass2), new FormatterConverter());    //使用SerializationInfo实例化一个名为infoClass2的实例
            infoClass2.AddValue("VarInt", 4);
            ObjectManager objMan =new ObjectManager(null,new StreamingContext(StreamingContextStates.All, null));
            object objSampleClass1 =FormatterServices.GetUninitializedObject(typeof(SampleClass1));                //
            object obj = infoClass1.GetValue("ObjSampleClass1", typeof(SampleStructure));                         //
            objMan.RegisterObject(obj, 2, null, 1, null);
            objMan.RecordFixup(2, typeof(SampleStructure).GetField("VarObject"), 3);
            object objSampleClass2 =FormatterServices.GetUninitializedObject(typeof(SampleClass2));//
            objMan.RegisterObject(objSampleClass2, 3, infoClass2);
            objMan.RegisterObject(objSampleClass1, 1, infoClass1);
            Console.WriteLine("VarInt = {0}, VarObject = {1}", ((SampleStructure)obj).VarInt,((SampleStructure)obj).VarObject == null ? "null" : "NOT null");
            objMan.DoFixups();
            Console.WriteLine("VarInt = {0}, VarObject = {1}",((SampleStructure)obj).VarInt, ((SampleStructure)obj).VarObject == null ? "null" : "NOT null");
            SampleClass1 root = (SampleClass1)objMan.GetObject(1);
            Console.WriteLine("VarInt = {0}, VarObject = {1}",root.ObjSampleClass1.VarInt,root.ObjSampleClass1.VarObject == null ? "null" : "NOT null");
            Console.ReadLine();
        }
    }
    [Serializable]
    class SampleClass1 : ISerializable                                   //
    {
        public SampleStructure ObjSampleClass1;

        SampleClass1(SerializationInfo info, StreamingContext context)
        {
            ObjSampleClass1 = (SampleStructure)info.GetValue("ObjSampleClass1", typeof(SampleStructure));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
         
        }
    }
   [Serializable]
    struct SampleStructure
    {
        public int VarInt;
        public object VarObject;
    }
    [Serializable]
    class SampleClass2 : ISerializable
    {
        public int VarInt;
        private SampleClass2(SerializationInfo info, StreamingContext context)
        {
            VarInt = info.GetInt32("VarInt");
        }
        public void GetObjectData(SerializationInfo info,StreamingContext context)
        {
           
        }
    }
}
有//的地方麻烦帮我解释下哈!!
搜索更多相关主题的帖子: void 
2012-04-10 18:05
昨日的忧伤
Rank: 1
来 自:福建三明
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-6-11
收藏
得分:0 
是关于数据的序列化
2012-04-10 22:02
快速回复:解释代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.040717 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved