private Type GetOriginalTypeParameterType(Type t)
{
Debug.Assert(t.IsGenericParameter);
int pos = t.GenericParameterPosition;
Type parentType = t.DeclaringType;
if (parentType != null && parentType.GetTypeInfo().IsGenericType)
{
parentType = parentType.GetTypeInfo().GetGenericTypeDefinition();
}
if (t.GetTypeInfo().DeclaringMethod != null)
{
MethodBase parentMethod = t.GetTypeInfo().DeclaringMethod;
if (parentType.GetGenericArguments() == null || pos >= parentType.GetGenericArguments().Length)
{
return t;
}
}
while (parentType.GetGenericArguments().Length > pos)
{
Type nextParent = parentType.DeclaringType;
if (nextParent != null && nextParent.GetTypeInfo().IsGenericType)
{
nextParent = nextParent.GetTypeInfo().GetGenericTypeDefinition();
}
if (nextParent != null && nextParent.GetGenericArguments() != null && nextParent.GetGenericArguments().Length > pos)
{
parentType = nextParent;
}
else
{
break;
}
}
return parentType.GetGenericArguments()[pos];
}