private static Type GetElementType(PropertyInfo propertyInfo)
{
Type elementType = null;
if (propertyInfo.PropertyType.IsArray) {
elementType = propertyInfo.PropertyType.GetElementType();
if (elementType == typeof(object)) {
throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Property {0} is not a strong-typed array.", propertyInfo.Name));
}
} else if (typeof(ICollection).IsAssignableFrom(propertyInfo.PropertyType)) {
// Locate Add method with 1 parameter
foreach (MethodInfo method in propertyInfo.PropertyType.GetMethods(BindingFlags.Public | BindingFlags.Instance)) {
if (method.Name == "Add" && method.GetParameters().Length == 1) {
ParameterInfo parameter = method.GetParameters()[0];
if (parameter.ParameterType == typeof(object)) {
throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Property {0} is not a strong-typed collection.", propertyInfo.Name));
} else {
elementType = parameter.ParameterType;
break;
}
}
}
if (elementType == null) {
throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "Invalid commandline argument type for property {0}.", propertyInfo.Name));
}
}
return elementType;
}