atifaziz (owner)

Revisions

gist: 49984 Download_button fork
public
Description:
KeyedCollection implementing ICustomTypeDescriptor
Public Clone URL: git://gist.github.com/49984.git
Embed All Files: show embed
DictionaryObject.cs #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#region Imports
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Collections.ObjectModel;
 
#endregion
 
public sealed class DictionaryObject<T> :
    KeyedCollection<string, KeyValuePair<string, T>>,
    ICustomTypeDescriptor
{
    PropertyDescriptorCollection _cachedProperties;
 
    public DictionaryObject() :
        this(null, null) { }
 
    public DictionaryObject(IEqualityComparer<string> comparer) :
        this(null, comparer) { }
 
    public DictionaryObject(IEnumerable<KeyValuePair<string, T>> dict) :
        this(dict, null) {}
 
    public DictionaryObject(IEnumerable<KeyValuePair<string, T>> dict, IEqualityComparer<string> comparer) :
        base(comparer ?? StringComparer.OrdinalIgnoreCase)
    {
        if (dict != null)
        {
            foreach (var pair in dict)
                Add(pair);
        }
    }
 
    public new T this[int index]
    {
        get { return base[index].Value; }
        
        set
        {
            var e = base[index];
            base[index] = new KeyValuePair<string, T>(e.Key, value);
        }
    }
 
    public new T this[string key]
    {
        get { return base[key].Value; }
        
        set
        {
            Remove(key);
            Add(key, value);
        }
    }
 
    public void Add(string key, T value)
    {
        Add(new KeyValuePair<string, T>(key, value));
    }
 
    protected override string GetKeyForItem(KeyValuePair<string, T> item)
    {
        return item.Key;
    }
 
    protected override void ClearItems()
    {
        ClearCachedProperties();
        base.ClearItems();
    }
 
    protected override void InsertItem(int index, KeyValuePair<string, T> item)
    {
        ClearCachedProperties();
        base.InsertItem(index, item);
    }
 
    protected override void RemoveItem(int index)
    {
        ClearCachedProperties();
        base.RemoveItem(index);
    }
 
    protected override void SetItem(int index, KeyValuePair<string, T> item)
    {
        ClearCachedProperties();
        base.SetItem(index, item);
    }
 
    private void ClearCachedProperties()
    {
        _cachedProperties = null;
    }
 
    AttributeCollection ICustomTypeDescriptor.GetAttributes()
    {
        return new AttributeCollection(null);
    }
 
    string ICustomTypeDescriptor.GetClassName() { return null; }
    string ICustomTypeDescriptor.GetComponentName() { return null; }
    TypeConverter ICustomTypeDescriptor.GetConverter() { return null; }
    EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() { return null; }
    PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() { return null; }
    object ICustomTypeDescriptor.GetEditor(Type editorBaseType) { return null; }
    EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes) { return GetEvents(); }
    EventDescriptorCollection ICustomTypeDescriptor.GetEvents() { return GetEvents(); }
    object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd) { return null; }
 
    private static EventDescriptorCollection GetEvents()
    {
        return new EventDescriptorCollection(null);
    }
 
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
    {
        if (attributes != null && attributes.Length > 0)
            throw new NotSupportedException();
 
        return GetProperties();
    }
 
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
    {
        return GetProperties();
    }
 
    private PropertyDescriptorCollection GetProperties()
    {
        if (_cachedProperties == null)
        {
            _cachedProperties = new PropertyDescriptorCollection(
                this.Select(e => new DictionaryPropertyDescriptor(e.Key)).ToArray());
        }
 
        return _cachedProperties;
    }
 
    private sealed class DictionaryPropertyDescriptor : PropertyDescriptor
    {
        public DictionaryPropertyDescriptor(string name) :
            base(name, null) { }
 
        public override bool CanResetValue(object component)
        {
            return false;
        }
 
        public override Type ComponentType
        {
            get { throw new NotImplementedException(); }
        }
 
        public override object GetValue(object component)
        {
            return ((DictionaryObject<T>) component)[Name];
        }
 
        public override bool IsReadOnly
        {
            get { return false; }
        }
 
        public override Type PropertyType
        {
            get { return typeof(T); }
        }
 
        public override void ResetValue(object component)
        {
            throw new NotSupportedException();
        }
 
        public override void SetValue(object component, object value)
        {
            ((DictionaryObject<T>) component)[Name] = (T) value;
        }
 
        public override bool ShouldSerializeValue(object component)
        {
            throw new NotSupportedException();
        }
    }
}