restsharp/RestSharp — Issue #177: Problem deserializing properties when class inherits from List<T> with the JSONDeserializer
Author: Stift
Created: 2011-11-03 13:12:59+00:00
State: open

When I deserialize a specialized class that inherits from List from a JSON Stream I get an error at line 296 of JsonDeserializer.cs.

The line is:
``` var itemType = type.GetGenericArguments()[0];
```

The problem is that the type itself is not (!) a generic, but one of its bases. A similar problem is desribed in issue 49 (https://github.com/restsharp/RestSharp/issues/49) for JSON so far, but the posted "solution" isn't very nice. It would be nice to expand the problematic line a little bit:

//Set the type and traverse up the inheritance tree to find the generic
Type genericType = type;
Type[] genericArguments = genericType.GetGenericArguments();
while (genericArguments.Length == 0)
{
genericType = genericType.BaseType;
if (genericType == null)
throw new InvalidOperationException("not a generic");

genericArguments = genericType.GetGenericArguments();
}
var itemType = genericArguments[0];
```

It would be really nice to find a clean solution in the next release.

BTW: Nice work so far!