4 using System.Collections;
5 using System.Collections.Generic;
7 using System.Reflection;
9 namespace SiliconStudio.Core.Reflection
16 private static readonly List<string> ListOfMembersToRemove =
new List<string> {
"Comparer",
"Keys",
"Values",
"Capacity" };
18 private readonly Type keyType;
19 private readonly Type valueType;
20 private readonly MethodInfo getEnumeratorGeneric;
21 private readonly PropertyInfo getKeysMethod;
22 private readonly PropertyInfo getValuesMethod;
23 private readonly PropertyInfo indexerProperty;
24 private readonly MethodInfo indexerSetter;
25 private readonly MethodInfo removeMethod;
26 private readonly MethodInfo containsKeyMethod;
37 if (!IsDictionary(type))
38 throw new ArgumentException(
@"Expecting a type inheriting from System.Collections.IDictionary",
"type");
40 Category = DescriptorCategory.Dictionary;
44 if (interfaceType != null)
46 keyType = interfaceType.GetGenericArguments()[0];
47 valueType = interfaceType.GetGenericArguments()[1];
48 IsGenericDictionary =
true;
49 getEnumeratorGeneric = typeof(
DictionaryDescriptor).GetMethod(
"GetGenericEnumerable").MakeGenericMethod(keyType, valueType);
50 containsKeyMethod = type.GetMethod(
"ContainsKey",
new[] { keyType });
54 keyType = typeof(
object);
55 valueType = typeof(
object);
56 containsKeyMethod = type.GetMethod(
"Contains",
new[] { keyType });
59 getKeysMethod = type.GetProperty(
"Keys");
60 getValuesMethod = type.GetProperty(
"Values");
61 indexerProperty = type.GetProperty(
"Item", valueType,
new[] { keyType });
62 indexerSetter = indexerProperty.SetMethod;
63 removeMethod = type.GetMethod(
"Remove",
new[] { keyType });
71 IsPureDictionary = Count == 0;
78 public bool IsGenericDictionary {
get;
private set; }
108 public bool IsPureDictionary {
get;
private set; }
129 if (dictionary == null)
throw new ArgumentNullException(
"dictionary");
130 if (IsGenericDictionary)
132 foreach (var item
in (
IEnumerable<KeyValuePair<object, object>>)getEnumeratorGeneric.Invoke(null,
new[] {dictionary}))
140 foreach (var keyValueObject
in simpleDictionary)
142 if (!(keyValueObject is DictionaryEntry))
144 throw new NotSupportedException(
"Key value-pair type [{0}] is not supported for IDictionary. Only DictionaryEntry".ToFormat(keyValueObject));
146 var entry = (DictionaryEntry)keyValueObject;
147 yield
return new KeyValuePair<object, object>(entry.Key, entry.Value);
159 public void SetValue(
object dictionary,
object key,
object value)
161 if (dictionary == null)
throw new ArgumentNullException(
"dictionary");
163 if (simpleDictionary != null)
165 simpleDictionary[key] = value;
170 if (indexerSetter == null)
172 throw new InvalidOperationException(
"No indexer this[key] method found on dictionary [{0}]".ToFormat(Type));
174 indexerSetter.Invoke(dictionary,
new[] { key, value });
183 public void Remove(
object dictionary,
object key)
185 if (dictionary == null)
throw new ArgumentNullException(
"dictionary");
187 if (simpleDictionary != null)
189 simpleDictionary.Remove(key);
194 if (removeMethod == null)
196 throw new InvalidOperationException(
"No Remove() method found on dictionary [{0}]".ToFormat(Type));
198 removeMethod.Invoke(dictionary,
new[] { key });
210 if (dictionary == null)
throw new ArgumentNullException(
"dictionary");
212 if (simpleDictionary != null)
214 return simpleDictionary.Contains(key);
216 if (containsKeyMethod == null)
218 throw new InvalidOperationException(
"No ContainsKey() method found on dictionary [{0}]".ToFormat(Type));
220 return (
bool)containsKeyMethod.Invoke(dictionary,
new[] { key });
229 return (
IEnumerable)getKeysMethod.GetValue(dictionary);
238 return (
IEnumerable)getValuesMethod.GetValue(dictionary);
246 public object GetValue(
object dictionary,
object key)
249 if (fastDictionary != null)
251 return fastDictionary[key];
254 return indexerProperty.GetValue(dictionary,
new [] { key });
269 return dictionary.Select(keyValue =>
new KeyValuePair<object, object>(keyValue.Key, keyValue.Value));
281 return base.PrepareMember(member);
override void Initialize()
override bool PrepareMember(MemberDescriptorBase member)
void SetValue(object dictionary, object key, object value)
Adds a a key-value to a dictionary.
A IMemberDescriptor for a PropertyInfo
IEnumerable GetKeys(object dictionary)
Returns an enumerable of the keys in the dictionary
Default implementation of a ITypeDescriptor.
IEnumerable GetValues(object dictionary)
Returns an enumerable of the values in the dictionary
bool ContainsKey(object dictionary, object key)
Indicate whether the dictionary contains the given key
Provides a descriptor for a System.Collections.IDictionary.
static bool IsDictionary(Type type)
Determines whether the specified type is a .NET dictionary.
IEnumerable< KeyValuePair< object, object > > GetEnumerator(object dictionary)
Gets a generic enumerator for a dictionary.
DictionaryDescriptor(ITypeDescriptorFactory factory, Type type)
Initializes a new instance of the DictionaryDescriptor class.
void Remove(object dictionary, object key)
Remove a key-value from a dictionary
bool IsReadOnly(object thisObject)
Determines whether the value passed is readonly.
Base class for IMemberDescriptor for a MemberInfo
object GetValue(object dictionary, object key)
Returns the value matching the given key in the dictionary, or null if the key is not found ...
A factory to create an instance of a ITypeDescriptor