Generic deserialization of an xml string

前端 未结 5 2046
名媛妹妹
名媛妹妹 2020-12-24 15:29

I have a bunch of different DTO classes. They are being serialized into an XML string at one point and shot over to client-side of the web app. Now when the client shoots

5条回答
  •  春和景丽
    2020-12-24 15:42

    if you have custom serialization/deserialization routine for each type, you could use something like this

    public T Deserialize (string xml)
    {
        if(typeof(T) == typeof(Person))
        {
            // deserialize and return Person instance
        }
        else if(typeof(T) == typeof(Address)
        {
            // deserialize and return Address instance
        }
        ...
        ...
        ...
    }
    

    And you can call

    Person p = Deserialize(personXmlStringFromClient);
    

提交回复
热议问题