一个很愚蠢的问题...无法在泛型类中创建枚举?
我试图让下面
public static class Component<TClass>where TClass : class
{
public static void Method<TEnum>() where TEnum : struct, IConvertible, IComparable, IFormattable
{
if (!typeof(TEnum).IsEnum)
{
throw new ArgumentException("TEnum must be an enum.");
}
if (myEnum.Equals(DependencyLifecycle.SingleInstance))
{
//do some thingh
}
}
}
public enum DependencyLifecycle
{
SingleInstance,
DoubleInstance
};
像一个泛型类,并试图援引它作为
class Program
{
static void Main(string[] args)
{
ConsoleApplication1.Component<Program>.Method<DependencyLifecycle.SingleInstance>();
}
}
,但我不能够正确去做吧。
错误
Error 1 'ConsoleApplication1.DependencyLifecycle.SingleInstance' is a 'field' but is used like a 'type'
Error 2 Operator '==' cannot be applied to operands of type 'System.Type' and 'ConsoleApplication1.DependencyLifecycle'
请详细说明“我没有能力做到这一点。”我们想知道您正在收到哪些错误,您尝试创建枚举时会发生什么,以及会发生什么。 – 2015-04-03 20:09:32
错误\t \t 1“ConsoleApplication1.DependencyLifecycle.SingleInstance”是“场”,而是用于像一个“类型” \t 错误\t \t 2操作“==”不能被应用于类型“的System.Type”和'的操作数ConsoleApplication1.DependencyLifecycle' – 2015-04-03 20:10:52
目前尚不清楚为什么你在这里使用泛型或你想要完成什么。是的,我们看到编译错误,但不知道您的意图是什么,建议如何解决问题并非易事。 – vcsjones 2015-04-03 20:16:48