c# - Is it possible to inherit from a class which is recursively generated? -
i have class makes nested grouping. simplified form follows.
public class group { public readonly object key; public readonly ienumerable<group> groups; public readonly ienumerable<datarow> drs; public group(object key, ienumerable<datarow> source, list<string> columnlist) { key = key; if (columnlist.count == 0) drs = source; else { string firstcolumn = columnlist.first(); list<string> restofcolumns = columnlist.skip(1).tolist(); groups = source.groupby(dr => dr[firstcolumn]) .select(g => new group(g.key, g, restofcolumns)); } } }
this fine point. need more properties(and methods) class. @ same time don't want add properties class, break simplicity , of usages not interested these properties. reason try inherit class.
public class specialgroup:group { public specialgroup(object key, ienumerable<datarow> source, list<string> columnlist) : base(key, source, columnlist) { } public void dosomething() { // } }
but time specialgroup
not being recursively generated(as expected). first main group specialgroup
, nested groups group
.
can i, way, inherit group
, still have recursion new type?
(without usage of extension methods please, not differ writing methods in group
class)
you change creation of group polymorphic. example:
public class group { public readonly object key; public readonly ienumerable<group> groups; public readonly ienumerable<datarow> drs; public group(object key, ienumerable<datarow> source, list<string> columnlist) { key = key; if (columnlist.count == 0) drs = source; else { string firstcolumn = columnlist.first(); list<string> restofcolumns = columnlist.skip(1).tolist(); groups = source.groupby(dr => dr[firstcolumn]) .select(g => creategroup(g.key, g, restofcolumns)); } } protected virtual group creategroup(object key, ienumerable<datarow> source, list<string> columnlist) { return new group(key, source, columnlist) } }
and in specialgroup
override return specialgroup
.
public class specialgroup:group { public specialgroup(object key, ienumerable<datarow> source, list<string> columnlist) : base(key, source, columnlist) { } public void dosomething() { // } protected override group creategroup(object key, ienumerable<datarow> source, list<string> columnlist) { return new specialgroup(key, source, columnlist) } }
this way, if root group of type specialgroup
children of type specialgroup
, otherwise group
.
Comments
Post a Comment