c# - Force derived class to implement base class constructor with parameter(s) -


is possible enforce compile-time contract on derived classes requiring implementation of constructor (with parameter)?

i have base class constructor requiring parameter:

public class foobase {   protected int value;   public foobase(int value) { this.value = value; }   public virtual void dosomething() { throw new notimplementedexception(); } } 

i'd force derivations of base class implement same constructor:

public class foo : foobase {   public foo(int value) : base(value) { }   public override void dosomething() { console.writeline("foo: {0}", value); } } 

if no constructor implemented, derived classes causes compiler error because there no default constructor in base class:

// error: 'does not contain constructor takes 0 arguments' // adding default constructor in foobase eliminates compiler error, // provides means instantiate class without initializing int value. public class foobar : foobase {   public override void dosomething() { console.writeline("foobar: {0}", value); } } 

adding default constructor, foobar(), in derived class silences compiler error, provides dangerous means of instantiating foobar without required base class int value being initialized. because i'm using factory (see below), silencing compiler error results in run-time error later. i'd force foobar implement foobar(int)

interesting observation:

if default constructor, foobase(), added foobase, 'inherited' derived classes not provide constructor:

  1. foo not inherit default constructor because supplies explicit constructor.
  2. foobar inherit foobase().

however, same not true non-default constructor foobase(int)!

  1. foo must explicitly implement foobase(int) , call base(int).
  2. foobar fails 'inherit' non-default constructor same way default constructor inherited!

i not want default constructor in base class because instances created using factory method supplies needed "settings" parameter. factory method not illustrated here (which uses activator.createinstance() method).

here way derived classes should instantiated:

  static void main(string[] args)   {     foobase myfoo = new foo(4);     // works, since foo(int) implemented.      // error: 'does not contain constructor takes 1 arguments'     foobase myfoobar = new foobar(9);  // fails compile.   } 

because using factory--not direct instantiation shown--there no compiler error. instead, runtime exception: 'constructor on type not found.'

unworkable solutions:

  • interfaces not support constructors.
  • constructors cannot virtual or abstract.

it appears supplying base class cannot enforce contract on constructors.

work-around:

  • provide default constructor in base class along property pass settings parameter.

if default constructor, foobase(), added foobase, 'inherited' derived classes not provide constructor:

this incorrect - constructors in general never inherited. default constructor automatically provided class not provide other constructor implementation.

you put in constraint on interface provides init() method you:

public interface iinit {    void init(int somevalue); }  public class foobase : iinit {    .. } 

Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -