I tried to implement the extension method for converting ArrayList object to a List. I referred to this.
namespace MyProjects.Extensions
{
static class ArrayListExtensions
{
/// <summary>
/// ArrayList => List<T>
/// </summary>
/// <remarks>
/// Based on http://www.dotnetperls.com/convert-arraylist-list
/// </remarks>
public static List<T> ToListCollection<T>(this ArrayList arrayList)
{
List<T> list = new List<T>(arrayList.Count);
foreach (T instance in arrayList)
{
list.Add(instance);
}
return list;
}
}
}
When I try to use it, I made sure that I added a using MyProjects.Extensions clause but it is still not recognized. Any ideas?
ArrayList arrTest = Settings.Default["ListA"] as ArrayList;
arrTest.ToListCollection<MyCustomObject>();
Make your extension class
publicand try. If its in different namespace it is not accessible to your current project. By default if there is no access modifier specified class areinternal.Do something like this
Read more about Access Modifiers on MSDN.
Note: Referred site has a demo in which the extension class is defined in same namespace as the consumer.
Alternatively you can use
arrayList.Cast<T>().ToList()(from Jon Skeet’s comment).Hope this helps you.