Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
int[] arr = {1, 2, 3};
var methods = typeof(IList<int>).GetMethods().Concat(typeof(IList).GetMethods());
int success = 0, failed = 0;
foreach (var methodInfo in methods)
{
try
{
var paramss = methodInfo.GetParameters().Select(x => x.DefaultValue).ToArray();
methodInfo.Invoke(arr, paramss);
Console.WriteLine("{0} successed", methodInfo.Name);
success++;
}
catch (Exception ex)
{
Console.WriteLine("{0} failed for reason {1}", methodInfo.Name, ex.GetType().Name);
failed++;
}
}
Console.WriteLine("Success = {0}\tFailed={1}", success, failed);
IMO there should be several more (generic) collection interfaces depending on the features of a collection. And the names should have been different too, List for something with an indexer is really stupid IMO.
- Just Enumeration IEnumerable<T>
- Readonly but no indexer (.Count, .Contains,...)
- Resizable but no indexer, i.e. set like (Add, Remove,...) current ICollection<T>
- Readonly with indexer (indexer, indexof,...)
- Constant size with indexer (indexer with a setter)
- Variable size with indexer (Insert,...) current IList<T>
I think the current collection interfaces are bad design. But since they have properties telling you which methods are valid(and this is part of the contract of these methods) it doesn't break the substitution principle.

C#: коллекции только для чтения и LSP