混合自定义和基本序列化? [英] Mixing custom and basic serialization?

查看:49
本文介绍了混合自定义和基本序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含超过 100 个属性的类(它是一个数据库映射类),其中一个属性必须在方法中.换句话说,这些数据不是通过属性公开的,而是通过方法公开的:

I've got a class with well over 100 properties (it's a database mapping class) and one of the properties has to be in a method. In other words this data is not exposed via a property but via methods:

"ABCType GetABC(), SetABC(ABCType value)"

"ABCType GetABC(), SetABC(ABCType value)"

这一切都非常不像 C#.看到的时候不寒而栗.

It's all very un-C#-like. I shudder when I see it.

该类需要可序列化,以便可以通过 Web 服务发送,并且 Get/Set 方法公开的数据也需要序列化.(它在一个方法中,因为我使用的网格使用反射做了一件奇怪的事情;它无法处理包含与包含对象相同类型的属性的对象.问题属性将数据库对象的原始状态存储在如果需要恢复.实施效率低下,是的 - 但我无法重新设计它.)

The class needs to be serializable so it can be sent over web services, and the data exposed by the Get/Set methods needs to be serialized too. (It's in a method because of a strange thing the grid I'm using does with reflection; it can't handle objects that contain properties of the same type as the containing object. The problem property stores the original state of the database object in case a revert is required. Inefficient implementation, yes - but I'm unable to re-engineer it.)

我的问题是:因为只有这 1 个字段需要自定义序列化代码,所以我只想使用自定义序列化来调用 GetABC 和 SetABC,为类的其余部分恢复到基本的 XML 序列化.它将最大限度地减少我的序列化代码中出现错误的可能性.有办法吗?

My question is this: since only this 1 field needs custom serialization code, I'd like to use custom serialization only for calling GetABC and SetABC, reverting to basic XML serialization for the rest of the class. It'll minimize potential for bugs in my serialization code. Is there a way?

推荐答案

我尝试的第一件事是添加用于序列化的属性,但将其从 UI 中隐藏:

The first thing I'd try is adding a property for serialization, but hiding it from the UI:

[Browsable(false)] // hide in UI
public SomeType ABC {
    get {return GetABC();}
    set {SetABC(value);}
}

不幸的是,您无法真正混合和匹配序列化;一旦你实现了IXmlSerializable,你就拥有了一切.如果您使用的是 WCF,则 DataContractSerialier 支持用于序列化的非公共属性,因此您可以使用:

You can't really mix and match serialization unfortunately; once you implement IXmlSerializable, you own everything. If you were using WCF, then DataContractSerialier supports non-public properties for serialization, so you could use:

[DataMember]
private SomeType ABC {
    get {return GetABC();}
    set {SetABC(value);}
}

但这不适用于通过 XmlSerializer 的asmx"网络服务.

but this doesn't apply for "asmx" web-services via XmlSerializer.

[Browsable] 技巧真的有效吗?假设自定义网格使用 TypeDescriptor,另一种选择可能是通过 ICustomTypeDescriptor 隐藏它,但这只是隐藏属性的大量工作......

Does the [Browsable] trick work at all? Assuming the custom grid uses TypeDescriptor, another option might be to hide it via ICustomTypeDescriptor, but that is a lot of work just to hide a property...

这篇关于混合自定义和基本序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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