简体   繁体   English

C#继承XmlSerializer

[英]C# inheritance XmlSerializer

greetngs, greetngs,

i have two classes: 我有两节课:

[Serializable]
public class FireGridUnit
{
    public GridUnit FireGridLocation { get; set; }

}

[Serializable]
public class FireResult: FireGridUnit
{
    public bool Hit { get; set; }

    public bool ShipDestroyed { get; set; }

    public Ship.ValidShips DestroyedShipType {get; set;}

    public bool Win { get; set; }
}

as you see FireResult inherits FireGrdUnit. 如您所见,FireResult继承了FireGrdUnit。

in my code when i try to use them i get a runtime error: 在我的代码中,当我尝试使用它们时,出现运行时错误:

Unable to cast object of type 'NietzscheBattleships.FireGridUnit' to type 'NietzscheBattleships.FireResult'. 无法将类型为“ NietzscheBattleships.FireGridUnit”的对象强制转换为类型为“ NietzscheBattleships.FireResult”。

is this because if i am serializing the class has to be independant ie not be inherited? 这是因为如果我要序列化该类必须是独立的,即不能被继承?

many thanks. 非常感谢。

EDIT: 编辑:

it happens here: 它发生在这里:

                    XmlSerializer ss;
                    StringWriter ww;

                    ss = new XmlSerializer(typeof(FireResult));
                    ww = new StringWriter();

                    ss.Serialize(ww, fireGridUnit);

                    MessageBox.Show("hello");

                    MessageBox.Show(ww.ToString());

it gives the error mentioned. 它给出了提到的错误。 if i take this out then it runs but obviously not doing what want it to do. 如果我把它取出来,它就会运行,但显然没有按照它想要的去做。

EDIT: i got it my mistake! 编辑:我明白了我的错误!

ss.Serialize(ww, fireGridUnit); should be ss.Serialize(ww, fireResult);

thanks. 谢谢。

The [Serializable] does nothing here ( XmlSerializer doesn't care); [Serializable]在这里什么也不做( XmlSerializer不在乎); assuming the real problem is in the serialization API (not just the cast), then: 假设真正的问题出在序列化API(而不仅仅是演员)中,则:

[XmlInclude(typeof(FireResult))]
public class FireGridUnit {...}

Then to use it, something like: 然后使用它,就像:

        XmlSerializer ser = new XmlSerializer(typeof(FireGridUnit));
        using (MemoryStream ms = new MemoryStream())
        {
            ser.Serialize(ms, new FireResult { Hit = true });
            ms.Position = 0;
            FireGridUnit fr = (FireGridUnit)ser.Deserialize(ms);
            Debug.WriteLine(fr is FireResult); // true
        }

No, it has nothing to do with [Serializable] . 不,与[Serializable]无关。 The way polymorphism works is the other way around. 多态性的工作方式则相反。 You can only upcast ( (ParentClass)childObject; ) through the hierarchy and not downcast ( (ChildClass)parentObject ). 您只能通过层次结构上(ParentClass)childObject;(ParentClass)childObject; ),而不能下(ChildClass)parentObject(ChildClass)parentObject )。

For instance: 例如:

//This is correct
FireResult result = new FireResult();
FireGridUnit unit = result;

//This just does not seem right
FireGridUnit unit = new FireGridUnit();
FireResult result = unit;

EDIT: Think of inheritance the following way. 编辑:通过以下方式考虑继承。 All FireResults are FireGridUnits, as such, you can convert FireResults to FireGridUnits. 所有FireResults都是FireGridUnit,因此,您可以将FireResults转换为FireGridUnits。 However, not all FireGridUnits are FireResult because, you could have a third class that inherits from FireGridUnit. 但是,并非所有FireGridUnit都是FireResult,因为您可能拥有从FireGridUnit继承的第三个类。 As such, you can not convert ALL FireGridUnits to a FireResult. 因此,您不能将所有FireGridUnits转换为FireResult。

Nope, you can't down-cast a FireGridUnit to a FireResult. 不,您不能将FireGridUnit转换为FireResult。 The other way around is fine. 另一种方法很好。 Either you serialized the wrong object or the cast you used on the return value of Deserialize() is wrong. 您序列化了错误的对象,或者在Deserialize()的返回值上使用的强制转换是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM