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:
- cast
ipersonlistlist(of iperson), calladdrange. work if ipersonlist implementations inheritlist(of iperson), it's not best option. - create
addrangeextension methodilist(t) - use loop instead of
addrange
Comments
Post a Comment