简体   繁体   中英

How to serialize a class with generic types using XmlSerializer

I am trying to serialize a generic class using XmlSerializer.

I want to serialize the TestClass irrespective of the Generic type, is it possible and how can I achieve this?

Please point me to some resources.

    public class CustomAttribute
    {
        public string Key { get; set; }
        public object Value { get; set; }
    }

    public class CustomAttribute<T> : CustomAttribute
    {
        [XmlIgnoreAttribute]
        public new T Value
        {
            get { return (T)base.Value; }
            set { base.Value = value; }
        }
    }

    public class TestClass
    {
        public List<CustomAttribute> AttributeList { get; set; }
    }


    class Program
    {
        static void Main(string[] args)
        {
            Type[] _extraTypes = new Type[] {typeof (CustomAttribute<string>)};

             var _testClass = new TestClass();

            _testClass.AttributeList = new List<CustomAttribute>();
            _testClass.AttributeList.Add(new CustomAttribute<string>{Key = "TestKey", Value = "a"});

            var serializer = new XmlSerializer(typeof(TestClass), _extraTypes);

            using (Stream str = new MemoryStream())
            {
                serializer.Serialize(str, _testClass);
            }

        }
    }

Try this

    public class CustomAttribute<T> : CustomAttribute
    {
        T _value;

        [XmlIgnoreAttribute]
        public new T Value
        {
            get { return _value; }
            set { base.Value = value;
                _value = value; }
        }
    }

To get all the different Types when you are serializing, you can easily loop over all attributes and get their Types:

public static void Main()
{

    var _testClass = new TestClass();

    _testClass.AttributeList.Add(new CustomAttribute<string>{Key = "TestKey", Value = "a"});
    _testClass.AttributeList.Add(new CustomAttribute<int>{Key = "TestKey", Value = 1337});
    _testClass.AttributeList.Add(new CustomAttribute<int>{Key = "TestKey", Value = 1});
    _testClass.AttributeList.Add(new CustomAttribute<int>{Key = "TestKey", Value = 6});
    _testClass.AttributeList.Add(new CustomAttribute<double>{Key = "TestKey", Value = 3.141592654});
    _testClass.AttributeList.Add(new CustomAttribute<string>{Key = "TestKey", Value = "x"});
    _testClass.AttributeList.Add(new CustomAttribute<bool>{Key = "TestKey", Value = true});

    // Build a new list with all the different Types
    List<Type> types = new List<Type>();
    // Loop over all Attributes in _testClass
    foreach (var a in _testClass.AttributeList) {
        // See if this Type is alreadyin the List
        if (types.Contains(a.GetType())) continue;
        // Add it to the List
        types.Add(a.GetType());
    }
    Type[] _extraTypes = types.ToArray();

    var serializer = new XmlSerializer(typeof(TestClass), _extraTypes);

    using (Stream str = new MemoryStream())
    {
        serializer.Serialize(str, _testClass);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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