c# - Avoid numerous casts in inheritance tree -


this problem seems super basic still can't find way clear it. when have simple inheritance b , c inheriting a

      | |-----| b     c 

let's these interface like:

public interface {     list<a> children { get; } } 

my issue: when got through b.children have cast b every time want use specifics. there way have list of children without having declare children in leaves of inheritance tree?

precision: child of same type parent (or sub-type). , tree can go deeper. example, think ui system objects, containers, controls, labels class has children things of same type or sub-types of itself.


here code. have top level as

public interface iterm {     string text { get; } } 

then, offered @damien_the_unbeliever

public interface iterm<t> : iterm t : iterm {     list<t> children { get; } }  public interface itermcontrol : iterm<itermcontrol> { ... }  public class termcontrol : itermcontrol { ... } 

i starting think useless have access list<iterm> iterm.children list<itermcontrol> itermcontrol.children. that's trying explain.

thanks

you might try doing this:

public interface a<t> t: a<t> {     list<t> children {get;} } 

which eric lippert describes in article curiouser , curiouser:

this c# variation on what's called curiously recurring template pattern in c++, , leave betters explain uses in language. pattern in c# attempt enforce usage of crtp.

and points out doesn't enforce correct types throughout - @ best, it's form of documentation rather prevents bad things happening.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -