private static Type GetProxyType(Type type)
{
// Get the DebuggerTypeProxyAttibute for obj
var attrs =
type.GetTypeInfo().CustomAttributes
.Where(a => a.AttributeType == typeof(DebuggerTypeProxyAttribute))
.ToArray();
if (attrs.Length != 1)
{
throw new InvalidOperationException(
string.Format("Expected one DebuggerTypeProxyAttribute on {0}.", type));
}
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)
{
proxyType = proxyType.MakeGenericType(genericArguments);
}
return proxyType;
}