Home > Code Generation > Code Generation – Create Descendant and Create Descendant (with virtual overrides)

Code Generation – Create Descendant and Create Descendant (with virtual overrides)

June 30th, 2011

The Create Descendant code provider shipped in CodeRush generates a descendant class for the active class, providing overrides for abstract members, if any. The second version of the code provider named Create Descendant (with virtual overrides), in addition to the Create Descendant, adds overrides for virtual members into a descendant class.

Applying the Create Descendant for the following abstract class:

public abstract class Logger
{
  public abstract void GetMessage(int index);
  public abstract void SendMessage(string msg);
}

Will generate the following descendant declaration:

public class LoggerDescendant : Logger
{
  public LoggerDescendant()
  {

  }
  public override void GetMessage(int index)
  {
    throw new NotImplementedException();
  }
  public override void SendMessage(string msg)
  {
    throw new NotImplementedException();
  }
}

If a class contains virtual members you can choose whether they should be overridden by applying the corresponding provider. Having the following class:

public class Calculator
{
  public virtual void Add() { }
  public virtual void Sub() { }
  public virtual void Mul() { }
  public virtual void Div() { }
}

Applying the Create Descendant (with virtual overrides) will result in generating the following class:

public class CalculatorDescendant : Calculator
{
  public CalculatorDescendant()
  {

  }
  public override void Add()
  {
    base.Add();
  }
  public override void Sub()
  {
    base.Sub();
  }
  public override void Mul()
  {
    base.Mul();
  }
  public override void Div()
  {
    base.Div();
  }
}

Both code providers add a default public parameterless constructor for the new class, and drop a marker at the starting source position. The new class is placed above the current class in the same source file.

—–
Products: CodeRush Pro
Versions: 11.1 and up
VS IDEs: any
Updated: Jun/30/2011
ID: C109

Similar Posts:

  1. June 4th, 2012 at 21:32 | #1

    Hi Alex. I have translated your nice article and published on the following URL:
    http://d.hatena.ne.jp/gsf_zero1/20120605/p2
    Arigato!,

  2. June 4th, 2012 at 22:35 | #2

    Thanks a lot!