c# - Convertir IList<T> a BindingList<T>
generics casting (5)
¿Cómo puedo emitir una lista de IList<Customer> a BindingList<Customer> ?
Desafortunadamente no puedes lanzar un IList a algo que no es. Sin embargo, puede crear una nueva lista de enlaces a partir de ella con bastante facilidad simplemente pasando su IList a su constructor.
BindingList<Customer> bindingList = new BindingList<Customer>(yourIList);
Información adicional: IBindingList hereda de IList : por lo tanto, IBindingList comparte todas las propiedades y firmas de funciones con IList . Por lo tanto, las implementaciones de IList pueden fácilmente "ajustarse" a IBindingList implementaciones de IBindingList .
BindingList constructor BindingList toma el parámetro IList , lo usa:
var binding = new BindingList<Customer>(list); //where list is type of IList<Customer>
var yourList = new List<Customer>();
var listBinding = new BindingList<Customer>(yourList);
No necesita hacer un lanzamiento, simplemente proporcione el constructor de la clase BindingList<T> con IList<T> , que tiene.
IList<Customer> list = new List<Customer>();
var bindingList = new BindingList<Customer>(list);