In the code below;
CustomClass<EnType1> a = new CustomClass<EnType1>();
CustomClass<TEnum<EnType1>> b = new CustomClass<TEnum<EnType1>>();
Throws error
The type
ConsoleApp2.TEnum<ConsoleApp2.EnType1>
cannot be used as type parameterT
in the generic type or methodCustomClass<T>
. There is no implicit reference conversion fromConsoleApp2.TEnum<ConsoleApp2.EnType1>
toSystem.Enum
. and The typeConsoleApp2.EnType1
cannot be used as type parameterT
in the generic type or methodCustomClass<T>
. There is no boxing conversion fromConsoleApp2.EnType1
toConsoleApp2.IEnum
.
How do I resolve this?
Basically I want to create a generic class which I should be able to use any Enum and then any class that inherits from IEnum
Any help appreciated
Full Code:
class Program
{
static void Main(string[] args)
{
CustomClass<EnType1> a = new CustomClass<EnType1>();
CustomClass<TEnum<EnType1>> b = new CustomClass<TEnum<EnType1>>();
TEnum<EnType2> t = new TEnum<EnType2>(EnType2.B);
Console.WriteLine("Hello World!" + t.EnumValue);
}
}
In this generic class I should be able to use any Enum and then any class that inherits from IEnum
public class CustomClass<T> where T : Enum, IEnum
{
public T Enum;
}
enum EnType1
{
A = 2,
B = 3
}
enum EnType2
{
A = 0,
B = 1
}
public interface IEnum
{
public int EnumValue { get; set; }
}
public class TEnum<T> : IEnum
{
public TEnum(T enumVal)
{
EnumValue = (int)(object)enumVal;
}
public int EnumValue { get ; set ; }
}