C# 序列化自定义BindableDictionary<;TKey,TValue>;? 问题:

C# 序列化自定义BindableDictionary<;TKey,TValue>;? 问题:,c#,serialization,C#,Serialization,如果这个自定义BindableDictionary实现了IDictionary和IBindingList,我将如何序列化它 活页词典 公共类BindableDictionary:IDictionary,IBindingList { 私有字典源=新字典(); void IBindingList.AddIndex(PropertyDescriptor属性){} 对象IBindingList.AddNew(){抛出新的NotImplementedException();} bool-IBindingL

如果这个自定义BindableDictionary实现了IDictionary和IBindingList,我将如何序列化它

活页词典
公共类BindableDictionary:IDictionary,IBindingList
{
私有字典源=新字典();
void IBindingList.AddIndex(PropertyDescriptor属性){}
对象IBindingList.AddNew(){抛出新的NotImplementedException();}
bool-IBindingList.AllowEdit{get{return false;}}
bool-IBindingList.AllowNew{get{return false;}}
bool IBindingList.AllowRemove{get{return false;}}
void IBindingList.ApplySort(PropertyDescriptor属性,ListSortDirection方向){}
int-IBindingList.Find(PropertyDescriptor属性,对象键){抛出新的NotImplementedException();}
bool-IBindingList.IsSorted{get{return false;}}
void IBindingList.RemoveIndex(PropertyDescriptor属性){}
void IBindingList.RemoveSort(){}
ListSortDirection IBindingList.SortDirection{get{return ListSortDirection.Ascending;}
PropertyDescriptor IBindingList.SortProperty{get{return null;}}
bool-IBindingList.SupportsChangeNotification{get{return true;}}
bool-IBindingList.supportsserching{get{return false;}}
bool-IBindingList.SupportsSorting{get{return false;}}
int System.Collections.IList.Add(对象值){抛出新的NotImplementedException();}
void System.Collections.IList.Clear(){Clear();}
bool System.Collections.IList.Contains(对象值){if(值为TKey){return source.ContainsKey((TKey)值);}else if(值为TValue){return source.ContainsValue((TValue)值);}return false;}
int System.Collections.IList.IndexOf(对象值){return-1;}
void System.Collections.IList.Insert(int索引,对象值){throw new NotImplementedException();}
bool System.Collections.IList.IsFixedSize{get{return false;}}
bool System.Collections.IList.IsReadOnly{get{return true;}}
void System.Collections.IList.Remove(对象值){if(值为TKey){Remove((TKey)值);}
void System.Collections.IList.RemoveAt(int index){throw new NotImplementedException();}
对象System.Collections.IList.this[int index]{get{throw new NotImplementedException();}set{throw new NotImplementedException();}}
私有ListChangedEventHandler listChanged;
事件ListChangedEventHandler IBindingList.ListChanged
{
添加{listChanged+=value;}
删除{listChanged-=value;}
}
受保护的虚拟void OnListChanged(ListChangedEventArgs e)
{
var-evt=listChanged;
如果(evt!=null)evt(this,e);
}
公共无效添加(TKey键,TValue值)
{
source.Add(键、值);
OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1));
}
公用门移除(TKey)
{
如果(源。删除(键))
{
OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1));
返回true;
}
返回false;
}
公共TValue此[TKey]
{
得到
{
返回源[键];
}
设置
{
源[键]=值;
OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1));
}
}
void ICollection.Add(KeyValuePair项)
{
((ICollection)来源)。添加(项);
OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1));
}
布尔ICollection.Remove(KeyValuePair项)
{
如果(((ICollection)源)。删除(项))
{
OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1));
返回true;
}
返回false;
}
public bool ContainsKey(TKey){return source.ContainsKey(key);}
公共ICollection键{get{return source.Keys;}}
public bool TryGetValue(TKey-key,out-TValue值){return source.TryGetValue(key,out-value);}
公共ICollection值{get{return source.Values;}}
public void Clear(){source.Clear();}
bool ICollection.Contains(KeyValuePair项){return((ICollection)source.Contains(项);}
void ICollection.CopyTo(KeyValuePair[]数组,int-arrayIndex){((ICollection)源).CopyTo(数组,arrayIndex);}
公共整数计数{get{return source.Count;}}
bool ICollection.IsReadOnly{get{return((ICollection)source).IsReadOnly;}
公共IEnumerator GetEnumerator(){返回源。GetEnumerator();}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator(){return GetEnumerator();}
bool ICollection.IsSynchronized{get{return false;}}
对象ICollection.SyncRoot{get{return null;}}
void ICollection.CopyTo(数组,int-arrayIndex){((ICollection)源).CopyTo(数组,arrayIndex);}
}

在这些情况下,通常需要做的是序列化数据。为此,我只需将数据放入一个
列表
,该列表可以很好地序列化

在这些情况下,您通常需要做的是序列化数据。为此,我只需将数据放入一个
列表
,该列表可以很好地序列化

你真的认为我们需要看所有的代码吗?你真的需要做这样的评论吗?你真的没有更好的话要说吗?真的很遗憾他们给了你MVP。你真的认为我们需要看所有的代码吗?你真的需要这样评论吗?你真的没有更好的话要说吗?他们给了你一个MVP真的很遗憾。
    public class BindableDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IBindingList
    {
        private Dictionary<TKey, TValue> source = new Dictionary<TKey, TValue>();

        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 { throw new NotImplementedException(); } 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);

            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); }
    }