'How to define Helper Class with generics and inheritance?
I'm trying to setup an helper class, that based on generics, should call methods in generic class type:
static class Helper<T> where T : IWorkingPhase
{
}
interface IWorkingPhase
{
GetListViewModel();
}
abstract class BaseWorkingPhase: IWorkingPhase
{
public abstract GetListViewModel()
{
}
}
class PhaseA : BaseWorkingPhase
{
public override GetListViewModel()
{
}
}
class PhaseB : BaseWorkingPhase
{
public override GetListViewModel()
{
}
}
What i'm trying to achieve is some similar to:
var a = Helper<PhaseB>.GetListViewModel();
How should i configure this all?
I was thinking of linking the generic with 'new()', but is this correct for a hypothetical future use of dependency injection?
Or should i set as static the "GetListViewModel" method, in the classes?
Solution 1:[1]
It looks like you are looking for:
static class Helper<T> where T : class, BaseWorkingPhase, new()
Or:
static class Helper<T> where T : BaseWorkingPhase
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | b166er |
