private void AssertIsValidArray(object instance, string property, int index)
{
Type instanceType = instance.GetType();
IList list = instance as IList;
bool validList = false;
if (list == null && instanceType.IsGenericType)
{
Type[] genArgs = instanceType.GetGenericArguments();
Type genList = typeof(System.Collections.Generic.IList<>).MakeGenericType(genArgs);
Type genTypeDef = instanceType.GetGenericTypeDefinition().MakeGenericType(genArgs);
validList = genList.IsAssignableFrom(genTypeDef);
}
if (!validList && list == null)
{
throw new RailsException("The property {0} is being accessed as " +
"an indexed property but does not seem to implement IList. " +
"In fact the type is {1}", property, instanceType.FullName);
}
if (index < 0)
{
throw new RailsException("The specified index '{0}' is outside the bounds " +
"of the array. Property {1}", index, property);
}
}