惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Attack and Defense Labs
Attack and Defense Labs
T
Tor Project blog
Scott Helme
Scott Helme
Schneier on Security
Schneier on Security
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Security @ Cisco Blogs
GbyAI
GbyAI
Help Net Security
Help Net Security
T
Troy Hunt's Blog
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta
L
LINUX DO - 最新话题
TaoSecurity Blog
TaoSecurity Blog
AWS News Blog
AWS News Blog
Y
Y Combinator Blog
SecWiki News
SecWiki News
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Google DeepMind News
Google DeepMind News
NISL@THU
NISL@THU
T
Threat Research - Cisco Blogs
S
Securelist
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
雷峰网
雷峰网
H
Help Net Security
AI
AI
The Hacker News
The Hacker News
B
Blog
罗磊的独立博客
L
Lohrmann on Cybersecurity
爱范儿
爱范儿
C
Cisco Blogs
有赞技术团队
有赞技术团队
Project Zero
Project Zero
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Secure Thoughts
A
Arctic Wolf
Blog — PlanetScale
Blog — PlanetScale
腾讯CDC
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Latest news
Latest news
Cloudbric
Cloudbric
D
Docker
博客园 - 叶小钗
月光博客
月光博客

博客园 - cjfwu

设计模式学习4-Bridge模式 设计模式学习3-Strategy模式 设计模式学习2-Adapter模式 设计模式学习1-Facade模式 设备控制(反馈处理) 通过System.IO.Packaging实现打包和解包 将目录添加环境变量 设备控制之矩阵状态显示 windows shell 编程3(函数解释) windows shell 编程2(浏览文件夹) windows shell 编程1(概念) 通过SvcUtil.exe生成客户端代码和配置 分组 在“添加引用”对话框中显示需要的Assembly 只运行一个实例 SVN操作 托盘操作 获得树节点的高度 枚举的操作
不同命名空间下名称和结构相同的类相互序列化与反序列化
cjfwu · 2010-06-12 · via 博客园 - cjfwu

首先建2个类,它们的命名空间不一样,但类名和结构相同:

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Domain1 {
public class Person {
public int Id { get; set; }
public string Name { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Domain2 {
public class Person {
public int Id { get; set; }
public string Name { get; set; }
}
}

然后在Program.cs里对Domain1.Person进行序列化:

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using Domain1;namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
Person person
= new Person { Id = 1, Name = "Who" };
XmlSerializer serializer
= new XmlSerializer(typeof(Person));
using (FileStream fs = new FileStream("d:\\person.xml", FileMode.Create)) {
serializer.Serialize(fs, person);
}

Console.WriteLine(

"ID:{0} Name:{1}", person.Id, person.Name);
Console.Read();
}
}
}

运行程序,会在D盘生成Domain1.Person对象的XML文件person.xml,

然后修改下程序,用Domain2.Person对D:\person.xml进行反序列化:

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using Domain2;namespace ConsoleApplication1 {
class Program {
static void Main(string[] args) {
Person person
= null;
XmlSerializer serializer
= new XmlSerializer(typeof(Person));
using (FileStream fs = new FileStream("d:\\person.xml", FileMode.Open)) {
person
= serializer.Deserialize(fs) as Person;
}

Console.WriteLine(

"ID:{0} Name:{1}", person.Id, person.Name);
Console.Read();
}
}
}

运行程序,成功反序列化。

如果在Domain1.Person类中增加或者减少属性,用Domain2.Person同样可以反序列化。