internal static void ValidateDebuggerTypeProxyProperties(Type type, object obj)
{
- Type proxyType = GetProxyType(type);
+ ValidateDebuggerTypeProxyProperties(type, type.GenericTypeArguments, obj);
+ }
+
+ internal static void ValidateDebuggerTypeProxyProperties(Type type, Type[] genericTypeArguments, object obj)
+ {
+ Type proxyType = GetProxyType(type, genericTypeArguments);
// Create an instance of the proxy type, and make sure we can access all of the instance properties
// on the type without exception
}
}
- internal static Type GetProxyType(object obj)
- {
- return GetProxyType(obj.GetType());
- }
+ public static Type GetProxyType(object obj) => GetProxyType(obj.GetType());
+
+ public static Type GetProxyType(Type type) => GetProxyType(type, type.GenericTypeArguments);
- private static Type GetProxyType(Type type)
+ private static Type GetProxyType(Type type, Type[] genericTypeArguments)
{
// Get the DebuggerTypeProxyAttibute for obj
var attrs =
}
CustomAttributeData cad = attrs[0];
- // Get the proxy type. As written, this only works if the proxy and the target type
- // have the same generic parameters, e.g. Dictionary<TKey,TValue> and Proxy<TKey,TValue>.
- // It will not work with, for example, Dictionary<TKey,TValue>.Keys and Proxy<TKey>,
- // as the former has two generic parameters and the latter only one.
Type proxyType = cad.ConstructorArguments[0].ArgumentType == typeof(Type) ?
(Type)cad.ConstructorArguments[0].Value :
Type.GetType((string)cad.ConstructorArguments[0].Value);
- var genericArguments = type.GenericTypeArguments;
- if (genericArguments.Length > 0)
+ if (genericTypeArguments.Length > 0)
{
- proxyType = proxyType.MakeGenericType(genericArguments);
+ proxyType = proxyType.MakeGenericType(genericTypeArguments);
}
return proxyType;
using System.Diagnostics;
using Xunit;
using Tests.Collections;
+using System.Reflection;
+using System.Linq;
namespace System.Collections.ObjectModel.Tests
{
- /// <summary>
- /// Tests the public methods in ReadOnlyDictionary<TKey, Value>.
- /// properly.
- /// </summary>
public class ReadOnlyDictionaryTests
{
/// <summary>
IDictionary<int, string> dictAsIDictionary = dictionary;
Assert.True(dictAsIDictionary.IsReadOnly, "ReadonlyDictionary Should be readonly");
+
+ IDictionary dictAsNonGenericIDictionary = dictionary;
+ Assert.True(dictAsNonGenericIDictionary.IsFixedSize);
+ Assert.True(dictAsNonGenericIDictionary.IsReadOnly);
}
/// <summary>
DebuggerAttributes.ValidateDebuggerDisplayReferences(new ReadOnlyDictionary<int, int>(new Dictionary<int, int>()).Keys);
DebuggerAttributes.ValidateDebuggerDisplayReferences(new ReadOnlyDictionary<int, int>(new Dictionary<int, int>()).Values);
}
+
+ [Fact]
+ public static void DebuggerAttribute_NullDictionary_ThrowsArgumentNullException()
+ {
+ TargetInvocationException ex = Assert.Throws<TargetInvocationException>(() => DebuggerAttributes.ValidateDebuggerTypeProxyProperties(typeof(ReadOnlyDictionary<int, int>), null));
+ ArgumentNullException argumentNullException = Assert.IsType<ArgumentNullException>(ex.InnerException);
+ Assert.Equal("dictionary", argumentNullException.ParamName);
+ }
+
+ [Fact]
+ public static void DebuggerAttribute_NullDictionaryKeys_ThrowsArgumentNullException()
+ {
+ TargetInvocationException ex = Assert.Throws<TargetInvocationException>(() => DebuggerAttributes.ValidateDebuggerTypeProxyProperties(typeof(ReadOnlyDictionary<int, int>.KeyCollection), new Type[] { typeof(int) }, null));
+ ArgumentNullException argumentNullException = Assert.IsType<ArgumentNullException>(ex.InnerException);
+ Assert.Equal("collection", argumentNullException.ParamName);
+ }
}
public class TestReadOnlyDictionary<TKey, TValue> : ReadOnlyDictionary<TKey, TValue>