一個很愚蠢的問題...無法在泛型類中創建枚舉?
我試圖讓下面
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