c# - Violating polymorphism by having base ViewModel class determine current type? -


i have base viewmodel has public method. things pretty simple @ first , role same of derived classes, want viewmodel different things depending on derived class calls it.

so example have:

    public void domethod()     {         dothismethod();     } 

we want like

    public void domethod()     {         if (this.gettype().name == "thisname")            dothismethod();         else            doanothermethod();     } 

is wrong this?

yes, solution propose wrong design point of view. base class should agnostic of inherited type "thisname". using naming resolve problem implementing in way:

public class viewmodel {     public virtual void domethod()     {         doanothermethod();     }      public void doanothermethod() {...} }  public class thisname: viewmodel {     public override void domethod()     {         dothismethod();     }      public void dothismethod() {...} } 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -