让我们假设我必须按级别将Car类的对象序列化.内部和公共.公共级别的某些属性不应该序列化,因为它们是内部的.
Let's suppose I have to serialize an object of a class Car in levels e.g. Internal and Public. Some of the properties in the Public level should not be serialized as they are internal.
目前,我想到的最简单的方法是使用继承:
At this moment the 'easiest' way I can think of to achieve this is by using inheritance:
class CarPublic { public int PropX {get;set} } class CarInternal: CarPublic { public string PropY {get;set} }那我可以
object ToSerialize() { CarInternal car = GetCar(); if( level == Level.Public ) { return car as CarPublic; } else { return car; } }ToSerialize()的结果由框架获取(我没有控制)并序列化为JSON或XML.
The result of the ToSerialize() is taken by a framework (I don't have control over) and serialized to JSON or XML.
为简单起见,我省略了XML序列化属性.
I omitted the XML serialization attributes for simplicity.
这感觉就像是骇客,骇客只会带您走远.有没有更好的方法(方式?)来实现这一目标?
This feels like a hack and hacks take you only so far. Is there better way (ways?) to achieve this?
我认为目前为止还很清楚,但是我想避免编写自己的JSON和XML序列化方法.
I think its clear by now, but I would like to avoid writing my own serialization methods for JSON and XML.
先谢谢了泰梅克
==编辑
为澄清起见,我希望能够序列化多个级别:
To clarify, I want to be able to serialize multiple levels:
class Car0 { public int PropA {get;set} } class Car1: Car0 { public string PropB {get;set} } class Car2: Car1 { public int PropC {get;set} } class Car3: Car2 { public string PropD {get;set} }和
object ToSerialize( Level level ) { Car3 car = GetCar(); switch( level ) { case Level.Zero: return car as Car0; case Level.One: return car as Car1; case Level.Two: return car as Car3; case Level.Three: return car as Car4; } return null; }==选择的方法
我将Marc Gravell的答案标记为答案,因为它提供了有关C#及其标准"组件如何支持我所要求的一般信息.
I marked Marc Gravell's answer as the answer, as it provides the generic information of how C# and it's 'standard' components support what I asked for.
但是,我认为解决问题的最佳方法是使用上面所示的代理类,使用下面所示的方法以这种多级模式对类进行序列化.
However I think the best approach for my problem is to use proxy classes as shown above and have the class being serialized in this multi-level pattern with methods like shown below.
public interface ICar { Car0 As0(); Car1 As1(); Car2 As2(); Car3 As3(); ... }这可以使 Car0..3 类非常简单,仅具有属性即可维护和理解.
This allows keeping the Car0..3 classes very simple, with only properties, to maintain and understand.
推荐答案这在很大程度上取决于您使用的序列化框架.您提到了xml和json-好吧,首先要注意的是,您可以使用以下命令进行修饰:
This depends a lot on what serialization framework you are using. You mention xml and json - well, the first thing to note is that you can just decorate with:
[XmlIgnore] public int PropX {get;set;}或
[ScriptIgnore] public int PropX {get;set;}XmlSerializer 和 JavascriptSerializer 将会响应.如果需要根据每个实例进行决策,则可以使用 ShouldSerialize * 和 * Specified 模式:
which XmlSerializer and JavascriptSerializer will respond to. If you need to make the decision on a per-instance basis, there is the ShouldSerialize* and *Specified patterns:
public bool ShouldSerializePropX() { // return true to serialize, false to omit }以上是基于名称的模式,由 XmlSerializer 等使用;它有一个双胞胎:
The above is a name-based pattern, that is used by XmlSerializer and others; it has a twin:
[XmlIgnore, Browsable(false)] public bool PropXSpecified { get { /* return true to serialize, false to omit */ } set { /* can just drop this value - don't need to assign */ } }您无需执行任何操作即可将它们连接起来-它们会自动工作.
You don't need to do anything to wire them up - they work automatically.
不同的序列化器允许使用不同的模式.
Different serializers allow different patterns.
此外,有时您可以在运行时添加诸如 [XmlIgnore] 之类的东西-例如通过 XmlAttributeOverrides 或任何给定序列化程序的等效项.
In addition, sometimes you can add things like [XmlIgnore] at runtime - for example via XmlAttributeOverrides, or the equivalent for any given serializer.
更多推荐
确定在运行时序列化哪些属性
发布评论