public static IDictionary GetReadOnlyDictionary(IDictionary dictionary) { Guard.ArgumentNotNull(dictionary, "dictionary"); OrderedDictionary result = new OrderedDictionary(); foreach (DictionaryEntry entry in dictionary) { result.Add(entry.Key, entry.Value); } return result.AsReadOnly(); }
private void Common (OrderedDictionary od) { Assert.IsNotNull (od.GetEnumerator (), "GetEnumerator"); Assert.AreEqual (0, od.Count, "Count-0"); Assert.IsFalse (od.IsReadOnly, "IsReadOnly"); od.Add ("a", "1"); Assert.AreEqual (1, od.Count, "Count-1"); od["a"] = "11"; Assert.AreEqual ("11", od["a"], "this[string]"); od[0] = "111"; Assert.AreEqual ("111", od[0], "this[int]"); DictionaryEntry[] array = new DictionaryEntry[2]; od.CopyTo (array, 1); Assert.AreEqual ("111", ((DictionaryEntry)array[1]).Value, "CopyTo"); Assert.AreEqual (1, od.Keys.Count, "Keys"); Assert.AreEqual (1, od.Values.Count, "Values"); Assert.IsTrue (od.Contains ("a"), "Contains(a)"); Assert.IsFalse (od.Contains ("111"), "Contains(111)"); od.Insert (0, "b", "2"); Assert.AreEqual (2, od.Count, "Count-2"); od.Add ("c", "3"); Assert.AreEqual (3, od.Count, "Count-3"); OrderedDictionary ro = od.AsReadOnly (); od.RemoveAt (2); Assert.AreEqual (2, od.Count, "Count-4"); Assert.IsFalse (od.Contains ("c"), "Contains(c)"); od.Remove ("b"); Assert.AreEqual (1, od.Count, "Count-5"); Assert.IsFalse (od.Contains ("b"), "Contains(b)"); od.Clear (); Assert.AreEqual (0, od.Count, "Count-6"); Assert.IsTrue (ro.IsReadOnly, "IsReadOnly-2"); // it's a read-only reference Assert.AreEqual (0, od.Count, "Count-7"); }
public void IsReadOnlyTests() { var d = new OrderedDictionary(); Assert.False(d.IsReadOnly); var d2 = d.AsReadOnly(); Assert.True(d2.IsReadOnly); }
public void AsReadOnlyTests() { var _d = new OrderedDictionary(); _d["foo"] = "bar"; _d[(object)13] = 37; var d = _d.AsReadOnly(); Assert.True(d.IsReadOnly); Assert.Equal("bar", d["foo"]); Assert.Equal(37, d[(object)13]); Assert.Throws<NotSupportedException>(() => { d["foo"] = "moooooooooaaah"; }); Assert.Throws<NotSupportedException>(() => { d["asdasd"] = "moooooooooaaah"; }); Assert.Equal(null, d["asdasd"]); Assert.Throws<ArgumentNullException>(() => { var a = d[null]; }); }
public void IsFixedSizeTests() { var d = new OrderedDictionary(); IDictionary dic = d; IDictionary rodic = d.AsReadOnly(); Assert.False(dic.IsFixedSize); Assert.True(rodic.IsFixedSize); }
public ModelDataMethodResult(object returnValue, OrderedDictionary outputParameters) { ReturnValue = returnValue; outputParameters = outputParameters ?? new OrderedDictionary(StringComparer.OrdinalIgnoreCase); _outputParameters = outputParameters.AsReadOnly(); }
public void GetObjectData_IEqualityComparer () { EqualityComparer comparer = new EqualityComparer (); OrderedDictionary coll = new OrderedDictionary (comparer); coll.Add ("a", "1"); coll.Add ("b", "2"); coll = coll.AsReadOnly (); SerializationInfo si = new SerializationInfo (typeof (OrderedDictionary), new FormatterConverter ()); coll.GetObjectData (si, new StreamingContext ()); foreach (SerializationEntry se in si) { switch (se.Name) { case "KeyComparer": Assert.AreSame (comparer, se.Value, se.Name); break; case "ReadOnly": Assert.IsTrue ((bool) se.Value, se.Name); break; case "InitialCapacity": Assert.AreEqual (0, se.Value, se.Name); break; case "ArrayList": Assert.AreEqual (2, ((object[]) se.Value).Length, se.Name); break; default: string msg = String.Format ("Unexpected {0} information of type {1} with value '{2}'.", se.Name, se.ObjectType, se.Value); Assert.Fail (msg); break; } } }
private ObjectDataSourceMethod GetResolvedMethodData(Type type, string methodName, Type dataObjectType, object oldDataObject, object newDataObject, DataSourceOperation operation) { Debug.Assert(dataObjectType != null, "This overload of GetResolvedMethodData should only be called when using a DataObject"); Debug.Assert(oldDataObject != null || newDataObject != null, "Did not expect both oldDataObject and newDataObject to be null"); // Get a list of all the overloads of the requested method MethodInfo[] methods = type.GetMethods( BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy); MethodInfo matchedMethod = null; ParameterInfo[] matchedMethodParameters = null; int requiredParameterCount; if (oldDataObject == null) { requiredParameterCount = 1; } else { if (newDataObject == null) { requiredParameterCount = 1; } else { requiredParameterCount = 2; } } foreach (MethodInfo mi in methods) { if (String.Equals(methodName, mi.Name, StringComparison.OrdinalIgnoreCase)) { if (mi.IsGenericMethodDefinition) { // We do not support binding to generic methods, e.g. public void DoSomething<T>(T t) continue; } ParameterInfo[] methodParameters = mi.GetParameters(); int methodParametersCount = methodParameters.Length; if (methodParametersCount == requiredParameterCount) { if (requiredParameterCount == 1 && methodParameters[0].ParameterType == dataObjectType) { // Only one parameter, of proper type // This is only valid for insert, delete, and non-optimistic update matchedMethod = mi; matchedMethodParameters = methodParameters; break; } if (requiredParameterCount == 2 && methodParameters[0].ParameterType == dataObjectType && methodParameters[1].ParameterType == dataObjectType) { // Two parameters of proper type in Update // This is only valid for optimistic update matchedMethod = mi; matchedMethodParameters = methodParameters; break; } } } } if (matchedMethod == null) { throw new InvalidOperationException(SR.GetString(SR.ObjectDataSourceView_MethodNotFoundForDataObject, _owner.ID, methodName, dataObjectType.FullName)); } Debug.Assert(matchedMethodParameters != null, "Method parameters should not be null if a method was found"); // Set up parameter array for method call OrderedDictionary parameters = new OrderedDictionary(2, StringComparer.OrdinalIgnoreCase); if (oldDataObject == null) { parameters.Add(matchedMethodParameters[0].Name, newDataObject); } else { if (newDataObject == null) { parameters.Add(matchedMethodParameters[0].Name, oldDataObject); } else { // We know that we matched on 2 objects for a optimistic update. // Match the parameters based on the format string so we know which one is the old // object and which is the new, then pass objects into the method in the correct order. string param0Name = matchedMethodParameters[0].Name; string param1Name = matchedMethodParameters[1].Name; string formattedParamName = String.Format(CultureInfo.InvariantCulture, OldValuesParameterFormatString, param0Name); if (String.Equals(param1Name, formattedParamName, StringComparison.OrdinalIgnoreCase)) { parameters.Add(param0Name, newDataObject); parameters.Add(param1Name, oldDataObject); } else { formattedParamName = String.Format(CultureInfo.InvariantCulture, OldValuesParameterFormatString, param1Name); if (String.Equals(param0Name, formattedParamName, StringComparison.OrdinalIgnoreCase)) { parameters.Add(param0Name, oldDataObject); parameters.Add(param1Name, newDataObject); } else { throw new InvalidOperationException(SR.GetString(SR.ObjectDataSourceView_NoOldValuesParams, _owner.ID)); } } } } // The parameters collection is always readonly in this case since we // do not want the user adding/removing the known objects. return new ObjectDataSourceMethod(operation, type, matchedMethod, parameters.AsReadOnly()); }
private ObjectDataSourceMethod GetResolvedMethodData(Type type, string methodName, Type dataObjectType, object oldDataObject, object newDataObject, DataSourceOperation operation) { int num; MethodInfo[] methods = type.GetMethods(BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance); MethodInfo methodInfo = null; ParameterInfo[] infoArray2 = null; if (oldDataObject == null) { num = 1; } else if (newDataObject == null) { num = 1; } else { num = 2; } foreach (MethodInfo info2 in methods) { if (string.Equals(methodName, info2.Name, StringComparison.OrdinalIgnoreCase) && !info2.IsGenericMethodDefinition) { ParameterInfo[] parameters = info2.GetParameters(); if (parameters.Length == num) { if ((num == 1) && (parameters[0].ParameterType == dataObjectType)) { methodInfo = info2; infoArray2 = parameters; break; } if (((num == 2) && (parameters[0].ParameterType == dataObjectType)) && (parameters[1].ParameterType == dataObjectType)) { methodInfo = info2; infoArray2 = parameters; break; } } } } if (methodInfo == null) { throw new InvalidOperationException(System.Web.SR.GetString("ObjectDataSourceView_MethodNotFoundForDataObject", new object[] { this._owner.ID, methodName, dataObjectType.FullName })); } OrderedDictionary dictionary = new OrderedDictionary(2, StringComparer.OrdinalIgnoreCase); if (oldDataObject == null) { dictionary.Add(infoArray2[0].Name, newDataObject); } else if (newDataObject == null) { dictionary.Add(infoArray2[0].Name, oldDataObject); } else { string name = infoArray2[0].Name; string a = infoArray2[1].Name; string b = string.Format(CultureInfo.InvariantCulture, this.OldValuesParameterFormatString, new object[] { name }); if (string.Equals(a, b, StringComparison.OrdinalIgnoreCase)) { dictionary.Add(name, newDataObject); dictionary.Add(a, oldDataObject); } else { b = string.Format(CultureInfo.InvariantCulture, this.OldValuesParameterFormatString, new object[] { a }); if (!string.Equals(name, b, StringComparison.OrdinalIgnoreCase)) { throw new InvalidOperationException(System.Web.SR.GetString("ObjectDataSourceView_NoOldValuesParams", new object[] { this._owner.ID })); } dictionary.Add(name, oldDataObject); dictionary.Add(a, newDataObject); } } return new ObjectDataSourceMethod(operation, type, methodInfo, dictionary.AsReadOnly()); }