Here's my problem. I've got generic Singleton class like this :
public abstract class Singleton<T> where T : Singleton<T>, new()
{
private static T instance;
public static T Instance
{
get
{
return instance == null ? new T() : instance;
}
}
}
And I wanna create a ProcessManager also is an generic abstract class but with enum generic parameter that inherit from this Singleton class. This is the code that currently I've made but with error:
public abstract class ProcessManager<TEnum> : Singleton<ProcessManager<TEnum>> where TEnum : Enum, new()
{
private TEnum state;
private Dictionary<TEnum, Action> stateInitializations = new Dictionary<TEnum, Action>();
public TEnum State
{
get { return state; }
set { InitializeState(state); }
}
private void InitializeState(TEnum _state)
{
stateInitializations[_state].Invoke();
}
}
There is a red underline at 'class ProcessManager' with error CS0310:
ProcessManager<TEnum>
must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'parameter' in the generic type or methodSingleton<T>
Anyone could tell me how to fix this plz?
CodePudding user response:
Since T
is a recursive constraint, you have to force any type that extends ProcessManager
to map their concrete type onto T
;
abstract class ProcessManager<T, E> : Singleton<T>
where T : ProcessManager<T, E>, new()
where E : Enum { ... }
class ConcreteType : ProcessManager<ConcreteType, EnumType> { ... }
CodePudding user response:
I tried two way to solve this problem:
- Remove ProcessManager's 'abstract'.
- Remove Singleton's ', new()' and also new T.
But second one doesn't make sense,, so I finally decided to use first one..
Anyway really thanks for those nice people who leave comment tried to help me sincerely.