工厂通过网络发送 xml 字符串

发布于 2024-11-05 02:19:02 字数 1767 浏览 1 评论 0原文

代码中解释了我要做什么...
但其要点是我想读取 xml 中保留的子类:

abstract class Parent {
    virtual string ToXml()
    {
        XmlSerializer xml = XmlSerializer( typeof (this) );
        ...
        return xmlString;
    }

    virtual void FromXml(string rawXml)
    {
        //either memberwise copy or throw exception if wrong type
        ...
    }
}

sealed class Child1 : Parent
{
    [XmlElement("Prop1")] 
    public Property1 { get; set; }

    public Child1() { }
}

sealed class Child2 : Parent
{
    [XmlElement("Prop2")]
    public Property1 { get; set; }

    public Child2() { }
}

static void main()
{
    string flatChild1 = new Child1().ToXml();
    string flatChild2 = new Child2().ToXml();

    // some time goes by
    ...

    Child1 one = new Child1();
    Child2 two = new Child2();

    one.FromXml(flatChild1); //must be "child one" string or exception
    two.FromXml(flatChild2);

    //one.FromXml(flatChild2); !! invalid

    /*
      what i want is some sort of factory...

      sealed class MyFactory
      {
         static Parent MyFactory.FromXml(string xmlObject);
      }

    */
    Parent obj1 = MyFactory.FromXml(flatChild1);
    Parent obj2 = MyFactory.FromXml(flatChild2);

    Assert.IsInstanceOfType(obj1, Child1.GetType());
    Assert.IsInstanceOfType(obj2, Child2.GetType());
}

** 编辑 **

我想通过网络发送 xml,所以像这样:

// Server.exe Project
server_SendChild1Message()
{
    byte[] data = Encoding.UTF16.GetBytes( child1.ToXml() );
    server.tcpClient[0].write( data, 0, data.Length );
}

// Seperate Client.exe
client_ReadMessages()
{
    string s = getNextXmlMessage();
    //needs to create a Child1 or Child2 based on xml string
    BaseXmlObj b = Factory.objFromXml(s);
}

What I am going for is explained in the code...
but the jist of it is I want to read child classes persisted in xml:

abstract class Parent {
    virtual string ToXml()
    {
        XmlSerializer xml = XmlSerializer( typeof (this) );
        ...
        return xmlString;
    }

    virtual void FromXml(string rawXml)
    {
        //either memberwise copy or throw exception if wrong type
        ...
    }
}

sealed class Child1 : Parent
{
    [XmlElement("Prop1")] 
    public Property1 { get; set; }

    public Child1() { }
}

sealed class Child2 : Parent
{
    [XmlElement("Prop2")]
    public Property1 { get; set; }

    public Child2() { }
}

static void main()
{
    string flatChild1 = new Child1().ToXml();
    string flatChild2 = new Child2().ToXml();

    // some time goes by
    ...

    Child1 one = new Child1();
    Child2 two = new Child2();

    one.FromXml(flatChild1); //must be "child one" string or exception
    two.FromXml(flatChild2);

    //one.FromXml(flatChild2); !! invalid

    /*
      what i want is some sort of factory...

      sealed class MyFactory
      {
         static Parent MyFactory.FromXml(string xmlObject);
      }

    */
    Parent obj1 = MyFactory.FromXml(flatChild1);
    Parent obj2 = MyFactory.FromXml(flatChild2);

    Assert.IsInstanceOfType(obj1, Child1.GetType());
    Assert.IsInstanceOfType(obj2, Child2.GetType());
}

** Edit **

I want to send xml over the wire, so something like this:

// Server.exe Project
server_SendChild1Message()
{
    byte[] data = Encoding.UTF16.GetBytes( child1.ToXml() );
    server.tcpClient[0].write( data, 0, data.Length );
}

// Seperate Client.exe
client_ReadMessages()
{
    string s = getNextXmlMessage();
    //needs to create a Child1 or Child2 based on xml string
    BaseXmlObj b = Factory.objFromXml(s);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谷夏 2024-11-12 02:19:02

泛型。这对你想做的事情有用吗?

如果您试图避免指定要反序列化的对象的类型,则不能;它确定用于反序列化对象的代码。

abstract class Parent<T> where T : Parent<T> {
    virtual string ToXml()
    {
        XmlSerializer xml = XmlSerializer( typeof (T) );
        ...
        return xmlString;
    }

    virtual void FromXml(string rawXml)
    {
        //either memberwise copy or throw exception if wrong type
        ...
    }
}

sealed class Child1 : Parent<Child1>
{
    [XmlElement("Prop1")] 
    public Property1 { get; set; }

    public Child1() { }
}

sealed class Child2 : Parent<Child1>
{
    [XmlElement("Prop2")]
    public Property1 { get; set; }

    public Child2() { }
}

static void main()
{
    string flatChild1 = new Child1().ToXml();
    string flatChild2 = new Child2().ToXml();

    // some time goes by
    ...

    Child1 one = new Child1();
    Child2 two = new Child2();

    one.FromXml(flatChild1); //must be "child one" string or exception
    two.FromXml(flatChild2);

    //one.FromXml(flatChild2); !! invalid

    /*
      what i want is some sort of factory...

      sealed class MyFactory<T>
      {
         static T MyFactory.FromXml(string xmlObject);
      }

    */
    Child1 obj1 = MyFactory<Child1>.FromXml(flatChild1);
    Child2 obj2 = MyFactory<Child2>.FromXml(flatChild2);

    Assert.IsInstanceOfType(obj1, Child1.GetType());
    Assert.IsInstanceOfType(obj2, Child2.GetType());
}

Generics. Does this work for what you are trying to do?

If you are trying to avoid specifing the type of the object to deserialize, you can't; it determines the code used to deserialize the object.

abstract class Parent<T> where T : Parent<T> {
    virtual string ToXml()
    {
        XmlSerializer xml = XmlSerializer( typeof (T) );
        ...
        return xmlString;
    }

    virtual void FromXml(string rawXml)
    {
        //either memberwise copy or throw exception if wrong type
        ...
    }
}

sealed class Child1 : Parent<Child1>
{
    [XmlElement("Prop1")] 
    public Property1 { get; set; }

    public Child1() { }
}

sealed class Child2 : Parent<Child1>
{
    [XmlElement("Prop2")]
    public Property1 { get; set; }

    public Child2() { }
}

static void main()
{
    string flatChild1 = new Child1().ToXml();
    string flatChild2 = new Child2().ToXml();

    // some time goes by
    ...

    Child1 one = new Child1();
    Child2 two = new Child2();

    one.FromXml(flatChild1); //must be "child one" string or exception
    two.FromXml(flatChild2);

    //one.FromXml(flatChild2); !! invalid

    /*
      what i want is some sort of factory...

      sealed class MyFactory<T>
      {
         static T MyFactory.FromXml(string xmlObject);
      }

    */
    Child1 obj1 = MyFactory<Child1>.FromXml(flatChild1);
    Child2 obj2 = MyFactory<Child2>.FromXml(flatChild2);

    Assert.IsInstanceOfType(obj1, Child1.GetType());
    Assert.IsInstanceOfType(obj2, Child2.GetType());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文