[英]serialize base class property in one derived class without serializing it in second serialize class
I am looking an issue, but i am still confuse how to tackle this. 我正在寻找一个问题,但是我仍然对如何解决这个问题感到困惑。 I have base class and there are some properties.
我有基类,并且有一些属性。 There are two derived classes from base class.
有两个从基类派生的类。
I want to serialize base class property in one derived class but i don't want to serialize same property in second serialize class. 我想在一个派生类中序列化基类属性,但是我不想在第二个序列化类中序列化相同的属性。
This is not actual code just for example purpose only. 这不是实际的代码,仅出于示例目的。
public class BaseRouter
{
private Double r;
private Double bch;
[XmlElement("BaseRouter.r")]
public double R { get { return r; } set { r = value; } }
}
First derived class 一阶派生类
public class CiscoRouter : BaseRouter
{
private String mRID;
[XmlAttribute("ID", Namespace = "rdf")]
public String MRID { get { return this.mRID; } set { mRID = value; } }
public Router()
{
}
Second derived class 二阶派生类
public class DellRouter : BaseRouter
{
private String mRID;
[XmlAttribute("ID", Namespace = "rdf")]
public String MRID { get { return this.mRID; } set { mRID = value; } }
public Router()
{
}
}
How can I avoid BaseRouter.r
in DellRouter
class and include in CiscoRouter
serialization? 如何避免
BaseRouter.r
在DellRouter
类,并包括在CiscoRouter
序列化?
Define an abstract
method in the base class, this will be used to do serialization/initialization. 在基类中定义一个
abstract
方法,该方法将用于进行序列化/初始化。 In the class where you want to serialize BaseRouter.r
serialize it in this methods implementation. 在要序列化
BaseRouter.r
的类中,在此方法实现中对其进行序列化。 In the other class, do whatever would be a logical substitute. 在另一类中,做任何可以替代逻辑的事情。
I believe you can implement a public ShouldSerializeR() function and override it in your DellRouter class to return false. 我相信您可以实现公共ShouldSerializeR()函数,并在DellRouter类中重写它以返回false。
class BaseRouter
{
...
public double R { get { return r; } set { r = value; } }
public virtual bool ShouldSerializeR() { return true; }
...
}
class DellRouter
{
...
public override bool ShouldSerializeR() { return false; }
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.