Generic deserialization of an xml string

前端 未结 5 2065
名媛妹妹
名媛妹妹 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:43

    Forget generics. What if you don't know the return type? If you're using Visual C# 2010 or later, this is the beauty of the new dynamic keyword. Below is an example serializer class that I wrote, and sample usage that takes only the XML string and tries to parse it for a general System type, returning back an output value on success. You can probably extend it for other, more custom types you have, but they probably need to have a default constructor and you would probably need to do some more parsing in order to get the type name and then get the path to it in your assembly. Its a bit tricky but once you get the hang of how the code below works, it starts to open up a bunch of possibilities. Isn't this exactly what you're looking for? Your question asks how to get an object of said type back without knowing that type (note however, that you still have to have a definition of that type in your code in order to deserialize it). Let me explain. If you look at how assemblyFormatter was used in the code below, you'll see that its trickier for a type you define yourself, like a struct or enum for example, because for these types you'd have to pass assemblyFormatter in as myObject.GetType().FullName. Thats the string that the activator uses to call the default constructor of your type to create it in order to be able to create a serializer off of it; it basically comes down to the complexity of having to know in your assembly that type definition in order to be able to deserialize it.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Linq;
    using System.Xml.Serialization;
    
    namespace DynamicSerializer
    {
        class Program
        {
            static void Main(string[] args)
            {
                bool myObject = true;
                // There are a bunch of other examples you can try out:
                // string myObject = "Hello, world.";
                // long myObject = 1000;
                // int myObject = 100;
                string mySerializedObject;
                if (Serializer.TrySerialize(myObject, out mySerializedObject))
                {
                    Console.WriteLine("Serialized {0} as {1}.", myObject, mySerializedObject);
                    dynamic myDeserializedObject;
                    if (Serializer.TryDeserialize(mySerializedObject, out myDeserializedObject))
                    {
                        Console.WriteLine("Deserialized {0} as {1}.", mySerializedObject, myDeserializedObject);
                    }
                }
                Console.ReadLine();
            }
    
            class Serializer
            {
                public static bool TrySerialize(dynamic unserializedObject, out string serializedObject)
                {
                    try
                    {
                        StringWriter writer = new StringWriter();
                        XmlSerializer serializer = new XmlSerializer(unserializedObject.GetType());
                        serializer.Serialize(writer, unserializedObject);
                        serializedObject = writer.ToString();
                        return true;
                    }
                    catch
                    {
                        serializedObject = null;
                        return false;
                    }
                }
    
                // The assemblyFormatter parameter is normally not passed in. However, it may be passed in for cases where the type is a special case (such as for enumerables or structs) that needs to be passed into the serializer. If this is the case, this value should be passed in as yourObject.GetType().FullName.
                public static bool TryDeserialize(string serializedObject, out dynamic deserializedObjectOut, string assemblyFormatter = "System.{0}")
                {
                    try
                    {
                        StringReader reader = new StringReader(serializedObject);
                        XDocument document = XDocument.Load(reader);
                        string typeString = null;
                        // Map the object type to the System's default value types.
                        switch (document.Root.Name.LocalName)
                        {
                            case "string":
                                typeString = "String";
                                break;
                            case "dateTime":
                                typeString = "DateTime";
                                break;
                            case "int":
                                typeString = "Int32";
                                break;
                            case "unsignedInt":
                                typeString = "UInt32";
                                break;
                            case "long":
                                typeString = "Int64";
                                break;
                            case "unsignedLong":
                                typeString = "UInt64";
                                break;
                            case "boolean":
                                typeString = "Boolean";
                                break;
                            case "double":
                                typeString = "Double";
                                break;
                            case "float":
                                typeString = "Single";
                                break;
                            case "decimal":
                                typeString = "Decimal";
                                break;
                            case "char":
                                typeString = "Char";
                                break;
                            case "short":
                                typeString = "Int16";
                                break;
                            case "unsignedShort":
                                typeString = "UInt16";
                                break;
                            case "byte":
                                typeString = "SByte";
                                break;
                            case "unsignedByte":
                                typeString = "Byte";
                                break;
                        }
                        if (assemblyFormatter != "System.{0}")
                        {
                            typeString = document.Root.Name.LocalName;
                        }
                        if (typeString == null)
                        {
                            // The dynamic object's type is not supported.
                            deserializedObjectOut = null;
                            return false;
                        }
                        if (typeString == "String")
                        {
                            // System.String does not specify a default constructor.
                            XmlSerializer serializer = new XmlSerializer(typeof(String));
                            reader = new StringReader(serializedObject);
                            deserializedObjectOut = serializer.Deserialize(reader);
                        }
                        else
                        {
                            object typeReference;
                            if (assemblyFormatter != "System.{0}")
                            {
                                typeReference = Activator.CreateInstance(Type.GetType(assemblyFormatter));
                            }
                            else
                            {
                                typeReference = Activator.CreateInstance(Type.GetType(String.Format(assemblyFormatter, typeString)));
                            }
                            XmlSerializer serializer = new XmlSerializer(typeReference.GetType());
                            reader = new StringReader(serializedObject);
                            deserializedObjectOut = serializer.Deserialize(reader);
                        }
                        return true;
                    }
                    catch
                    {
                        deserializedObjectOut = null;
                        return false;
                    }
                }
            }
        }
    }
    

提交回复
热议问题