.Net 中的 XML 序列化是否区分大小写? [英] Is XML Serialization in .Net case sensitive?

查看:28
本文介绍了.Net 中的 XML 序列化是否区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有找到任何明确说明 XML 序列化是否区分大小写的文档.

I did not find any documentation which explicitly states whether XML serialization is case-sensitive or not.

假设我有以下课程:

class Test
{
public int Field;
public string field;
}

XML 序列化这个类时会不会有问题,因为它包含两个名称相同但大小写不同的字段?

Will I have any problems when XML serializing this class becuase it contains two fields having same name differing only in case?

澄清:我知道有两个同名字段仅因大小写而异是不好的,我不是在设计这样的野兽.我正在与被另一个人放纵的野兽搏斗.

CLARIFICATION: I KNOW its bad to have two fields with same name differing only by case and i am NOT designing such a beast. I am battling a beast let loose on me by another person.

推荐答案

简短的回答是序列化保留大小写并且类将在 C# 中成功序列化(假设您将类设为 public - as是,XmlSerializer 会窒息.

The short answer is that serialization preserves case and the class will be successfully serialized in C# (assuming you make the class public - as is, the XmlSerializer will choke on it).

但如果你不得不问这个问题,那么课程设计就存在严重问题.拥有两个 public 属性或字段,名称相同,只是大小写不同,每个属性或字段执行或影响不同的功能,这是一件可怕的事情.在这种情况下,字段甚至不是同一类型.

But if you have to ask this question at all, then there's something seriously wrong with the class design. It is a horrible, horrible thing to have two public properties or fields, with the same name differing only by case, that each perform or affect different functionality. In this case, the fields aren't even the same type.

如果你下载了一个 3rd 方库,它给了你一个这样的类,你会怎么做?你怎么知道哪个是哪个?你会如何记录这个,或者在文档中查找它?只是不要这样做 - 不要让两个成员出于所有意图和目的具有相同的名称.唯一的例外是公共属性的私有支持字段 - 在这种情况下,两者非常密切相关,因此问题不大(尽管有些人仍然喜欢使用下划线作为前缀).

If you downloaded a 3rd-party library and it handed you a class like this, what would you do? How would you know which is which? How would you even document this, or look it up in the documentation? Just don't do it - don't have two members that have, for all intents and purposes, the same name. The only exception to this is a private backing field for a public property - in that case, the two are very closely related, so it's not as much of a problem (although some people still prefer to prefix with underscores).

正如 John 在评论中提到的,即使 C# 区分大小写,其他 .NET 语言不是,所以您可以将其序列化为 XML,但不能保证其他人能够将其反序列化到他们的环境中.

And as John mentions in the comment, even though C# is case-sensitive, other .NET languages are not, so you might be able to serialize this to XML but there's no guarantee that someone else will be able to deserialize it into their environment.

重新考虑你的设计,所以它没有看起来像这样的类.

Rethink your design, so it doesn't have classes that look like this.

这篇关于.Net 中的 XML 序列化是否区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆