c# - If abstract base class contains parameterised constructor (and derived does not) why can't it be used? -


i have ddd type solution "domain model" classes constructed using "dto" class (i.e. raw data db).

the domain model classes inherit abstract base class, intended provide generic injecting/retrieving of dto data. here sample:

public abstract class domainmodelbase<t> t : idto, new() {     protected t _data;      protected domainmodelbase()     {         _data = new t();     }      protected domainmodelbase(t data)     {         _data = data;     }      protected void setdata(t data)     {         _data = data;     }      public t getdata()     {         return _data;     } }  public class attributeoption : domainmodelbase<attributeoptiondata> {     //public attributeoption(attributeoptiondata data)     //{     //    setdata(data);     //} } 

i thought (because domainmodelbase contains parameterised constructor) able this:

        var data = new attributeoptiondata();         var model = new attributeoption(data); 

however, compiler says "constructor 'attributeoption' has 0 parameters, invoked 1 argument". way make work seems to create parameterised constructor in derived class (like commented out 1 above).

is there way make work modifying base class, i.e. without work of setting parameterised constructors in every derived class?

no, can't.

it's limitation says 'each derived class should use (implicitly or explicitly) @ least 1 constructor base class.

in example, child class implicitly has parameterless constructor implicitly uses parameterless constructor base.

so, need to: either setup parameretised constructor in every derived class or delele constructor base class.

or, can try that:

public class attributeoption : domainmodelbase<attributeoptiondata> {     public attributeoption(attributeoptiondata data) : base(data) { } } 

that's not want, have.


Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -