vb.net - Cannot use AddRange on a list with an interface -


this vb in .net4, have same problem in c#. have use interfaces reasons won't go into.

this works, doesn't use interfaces:

public class personlist    inherits list(of person) end class  dim mypeople new personlist mypeople.addrange(somelist) 

i need use interfaces such iperson , ipersonlist because use dependency injection enable testing of these classes.

when create interface personlist, can't see addrange more.

new version of personlist using interface

public class personlist    inherits list(iperson)    implements ilist(of iperson)    implements ipersonlist end class  interface list: public interface ipersonlist    inherits ilist(of iperson) end interface 

everthing builds , works expected until try call addrange:

dim mypeople ipersonlist = new personlist mypeople.addrange(somelist) 'build error addrange not member of ipersonlist 

presumably ipersonlist needs inherit has implementation of addrange. addrange part of list(of t), interface cannot inherit from.

so how list interface expose addrange?

you could:

  1. cast ipersonlist list(of iperson), call addrange. work if ipersonlist implementations inherit list(of iperson), it's not best option.
  2. create addrange extension method ilist(t)
  3. use loop instead of addrange

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -