C # interface / abstract class with dynamic type

I am writing a series of math class, each of which inherits from an abstract class. I want my abstract class to have a getter and setter called Parameters.

public abstract dynamic Parameters {get; set;} 

Then, in each individual math class, I want to implement parameters with a specific type:

If a class needs a period, I would do it like this:

 public override IPeriod Parameters {get; set;} 

This does not compile. Obviously, I could change the return type to dynamic , and this will work, but then I will lose intellisense. Is there a standard way to do this without losing intellisense?

Each of the classes will have {get; set;} but they will be of a different type. Is it better to just exclude parameters from an abstract class?

+4
source share
2 answers

Yes, use generics ..

 public MyAbstractBaseClass<T> { public abstract T Parameters {get; set;} } 

then you can inherit the job of the type that will be used for param, for example ..

 public PeriodClass : MyAbstractBaseClass<IPeriod> { public override IPeriod Parameters {get; set;} } 
+11
source

If you make the parameters generic, you can return whatever type you want:

 public abstract T Parameters<T> {get; set;} 
+2
source

Source: https://habr.com/ru/post/1333264/


All Articles