简体   繁体   English

C#-序列化自定义词典 <TKey,TValue> 对象和TValue子对象

[英]C# - Serializing Custom Dictionary<TKey,TValue> Object & TValue SubObjects

Questions - (Relative Code Below) 问题-(下面的相对代码)

  • I have a custom class which is a BindableDictionary(). 我有一个自定义类,它是BindableDictionary()。
  • I want to make it serializable. 我想使其可序列化。
  • How would I Serialize this? 我该如何序列化?

Thoughts 思想

  • Will The serialization still work if the TValue of the BindableDictionary is a custom object that has another BindableDictionary as a property? 如果BindableDictionary的TValue是具有另一个BindableDictionary作为属性的自定义对象,则序列化仍将有效吗?
  • So my question is, will both the Top Level BindableDictionary, and the BindableDictionary property of the Top Levels TValue both serialize correctly? 所以我的问题是,顶级TValue的顶级BindableDictionary和BindableDictionary属性都可以正确序列化吗?

     public class MyClass { public string Name { get; set; } public BindableDictionary<string, MySubClass> SubDictionary { get; private set; } public MyClass(string name) { Name = name; SubDictionary = new BindableDictionary<string, MySubClass>(); } } public class MySubClass { public int Age { get; set; } public double Height { get; set; } public double Weight { get; set; } public MySubClass() { } public MySubClass(int age,double height, double weight) { Age = age; Height = height; Weight = weight; } } 

Class To Serialize 类进行序列化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections;

namespace CommTraderCommon.HelperObjects
{
    public class BindableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IBindingList
    {
        private Dictionary<TKey, TValue> source = new Dictionary<TKey, TValue>();
        private Dictionary<int, TKey> sourceKey = new Dictionary<int, TKey>();

        void IBindingList.AddIndex(PropertyDescriptor property) { }
        object IBindingList.AddNew() { throw new NotImplementedException(); }
        bool IBindingList.AllowEdit { get { return false; } }
        bool IBindingList.AllowNew { get { return false; } }
        bool IBindingList.AllowRemove { get { return false; } }
        void IBindingList.ApplySort(PropertyDescriptor property, ListSortDirection direction) { }
        int IBindingList.Find(PropertyDescriptor property, object key) { throw new NotImplementedException(); }
        bool IBindingList.IsSorted { get { return false; } }
        void IBindingList.RemoveIndex(PropertyDescriptor property) { }
        void IBindingList.RemoveSort() { }
        ListSortDirection IBindingList.SortDirection { get { return ListSortDirection.Ascending; } }
        PropertyDescriptor IBindingList.SortProperty { get { return null; } }
        bool IBindingList.SupportsChangeNotification { get { return true; } }
        bool IBindingList.SupportsSearching { get { return false; } }
        bool IBindingList.SupportsSorting { get { return false; } }
        int System.Collections.IList.Add(object value) { throw new NotImplementedException(); }
        void System.Collections.IList.Clear() { Clear(); }
        bool System.Collections.IList.Contains(object value) { if (value is TKey) { return source.ContainsKey((TKey)value); } else if (value is TValue) { return source.ContainsValue((TValue)value); } return false; }
        int System.Collections.IList.IndexOf(object value) { return -1; }
        void System.Collections.IList.Insert(int index, object value) { throw new NotImplementedException(); }
        bool System.Collections.IList.IsFixedSize { get { return false; } }
        bool System.Collections.IList.IsReadOnly { get { return true; } }
        void System.Collections.IList.Remove(object value) { if (value is TKey) { Remove((TKey)value); } }
        void System.Collections.IList.RemoveAt(int index) { throw new NotImplementedException(); }

        object System.Collections.IList.this[int index]
        {
            get
            {
                return source[sourceKey[index]];
            }
            set { throw new NotImplementedException(); }
        }

        private ListChangedEventHandler listChanged;

        event ListChangedEventHandler IBindingList.ListChanged
        {
            add { listChanged += value; }
            remove { listChanged -= value; }
        }

        protected virtual void OnListChanged(ListChangedEventArgs e)
        {
            var evt = listChanged;

            if (evt != null) evt(this, e);
        }

        public void Add(TKey key, TValue value)
        {
            source.Add(key, value);
            sourceKey.Add(sourceKey.Count, key);
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }

        public bool Remove(TKey key)
        {
            if (source.Remove(key))
            {
                OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));

                return true;
            }

            return false;
        }

        public TValue this[TKey key]
        {
            get
            {
                return source[key];
            }
            set
            {
                source[key] = value;

                OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
            }
        }

        void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
        {
            ((ICollection<KeyValuePair<TKey, TValue>>)source).Add(item);

            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }

        bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
        {
            if (((ICollection<KeyValuePair<TKey, TValue>>)source).Remove(item))
            {
                OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));

                return true;
            }

            return false;
        }

        public bool ContainsKey(TKey key) { return source.ContainsKey(key); }
        public ICollection<TKey> Keys { get { return source.Keys; } }
        public bool TryGetValue(TKey key, out TValue value) { return source.TryGetValue(key, out value); }
        public ICollection<TValue> Values { get { return source.Values; } }
        public void Clear() { source.Clear(); }
        bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) { return ((ICollection<KeyValuePair<TKey, TValue>>)source).Contains(item); }
        void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) { ((ICollection<KeyValuePair<TKey, TValue>>)source).CopyTo(array, arrayIndex); }
        public int Count { get { return source.Count; } }
        bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly { get { return ((ICollection<KeyValuePair<TKey, TValue>>)source).IsReadOnly; } }
        public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return source.GetEnumerator(); }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }
        bool ICollection.IsSynchronized { get { return false; } }
        object ICollection.SyncRoot { get { return null; } }
        void ICollection.CopyTo(Array array, int arrayIndex) { ((ICollection)source).CopyTo(array, arrayIndex); }
    }
}

Serialization is not a problem if you only serialize objects that are marked with the Serializable attribute. 如果仅序列化标记有Serializable属性的对象,则序列化不是问题。 In your code I do not see this attribute (yet) so your BindableDictionary is not serializable. 在您的代码中,我尚未看到此属性(因此),因此您的BindableDictionary无法序列化。

There shouldn't be a problem serializing objects that refer to objects of the same type (ie when TValue is a BindableDictionary ). 序列化引用相同类型对象的对象应该没有问题(即,当TValueBindableDictionary )。

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

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