C# 将未知枚举值传递给函数

C# 将未知枚举值传递给函数,c#,reflection,enumerator,C#,Reflection,Enumerator,这是对这个问题的阐述: 我创建了一个小示例应用程序来介绍我的问题: 更新:这是C#编程语言的一个已知难题。我在代码中为在搜索引擎中找到它的人添加了使用过的变通方法 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FlexibleEnums { class Program { public enum Color

这是对这个问题的阐述:

我创建了一个小示例应用程序来介绍我的问题:

更新:这是C#编程语言的一个已知难题。我在代码中为在搜索引擎中找到它的人添加了使用过的变通方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FlexibleEnums
{
    class Program
    {
        public enum Color
        {
            Blue,
            Red,
            Green
        };

        static void Main(string[] args)
        {
            CheckEnum<Color>();
            Console.ReadKey();
        }

        private static void CheckEnum<T>()
        {
            foreach (T item in Enum.GetValues(typeof(T)))
            {
                Console.WriteLine(item);

                // And here is the question:
                // I would like to uncheck this line, but that does not compile!
                //DoSomethingWithAnEnumValue(item);

                // Solution:
                // Not so nice, but it works.
                // (In the real program I also check for null off cource!)
                DoSomethingWithAnEnumValue(item as Enum);


            }

        }

        private static void DoSomethingWithAnEnumValue(Enum e)
        {
            Console.WriteLine(e);
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间flexibleEnum
{
班级计划
{
公共枚举颜色
{
蓝色
红色
绿色
};
静态void Main(字符串[]参数)
{
CheckEnum();
Console.ReadKey();
}
私有静态void CheckEnum()
{
foreach(Enum.GetValues中的T项(typeof(T)))
{
控制台写入线(项目);
//问题是:
//我想取消选中这一行,但这不会编译!
//DOSOMEthingWithannumValue(项目);
//解决方案:
//虽然不太好,但很管用。
//(在真实的程序中,我还检查空关cource!)
doSomethingWithannumValue(项作为枚举);
}
}
私有静态void DoSomethingWithAnEnumValue(枚举e)
{
控制台写入线(e);
}
}
}
我认为我应该做一些类似的事情:

private static void CheckEnum<T>() where T : Enum
private static void CheckEnum(),其中T:Enum
但这也给了我编译错误

谢谢你的帮助

将泛型条件限制为
Enum
,这是不可能的(当我遇到同样的问题时,我找不到方法)

更新 您可以获得的最接近于
ValueType
struct

其中T:ValueType


注意:很抱歉,是的,由于n8wrl,它是
struct

您还需要确保键入的DoSomethingWithanNumeraterValue(项)与您的T:

private static void DoSomethingWithAnEnumeratorValue<T>(T item) where T : ...
{
}
private static void dosomething带有numeratorvalue(T项),其中T:。。。
{
}

其中T的限制方式与CheckEnumerator的限制方式相同。

我认为这个问题可以重新表述为“如何对枚举值设置通用约束”

Jon Skeet在这里写过关于它的博客:

自C#7.3以来,有人提出了这个问题,可以创建枚举约束:

公共密封类MyClass:MyBaseClass,imy接口,其中T:Enum
{
公共MyClass(Args Args)
:基准(args)
{
}
}
我需要使用者传入几个枚举中的一个,其中枚举必须存在于特定的命名空间中。如果使用了不同的枚举,则引发运行时异常:

public sealed class MyClass<T> : MyBaseClass, IMyInterface<T> where T : Enum
{
        public MyClass(Args args)
            : base(args)
        {
            var t = typeof(T);
            if (t.Namespace.IsNotContaining("My.Namespace.Of.Interest"))
            {
#pragma warning disable CA1303 // Do not pass literals as localized parameters
                throw new InvalidEnumArgumentException("The enum provided was not of the expected type.");
#pragma warning restore CA1303 // Do not pass literals as localized parameters
            }
        }
}

公共密封类MyClass:MyBaseClass,imy接口,其中T:Enum
{
公共MyClass(Args Args)
:基准(args)
{
var t=类型(t);
if(t.Namespace.IsNotContaining(“My.Namespace.Of.Interest”))
{
#pragma warning disable CA1303//不要将文字作为本地化参数传递
抛出新的InvalidEnumArgumentException(“提供的枚举不是预期的类型”);
#pragma warning restore CA1303//不要将文字作为本地化参数传递
}
}
}

Hmmm。这给了我与枚举相同的错误:约束不能是特殊类“System.ValueType”次要的诡辩,但枚举(Enum)和枚举数(IEnumerable提供的IEnumerator)之间有区别。为了保持一致性,您可能需要使用术语Enum(这是很好理解的)或枚举(虽然这也可能意味着使用枚举器进行枚举)。同意,正如我在下面的回答中所说,我认为更准确的表达问题的方式是“如何对枚举值设置通用约束”您是否尝试过Google“generic parameter restrict enum”?仅此一项就有很多结果。好的,感谢您提供有关此主题的更多信息。
public sealed class MyClass<T> : MyBaseClass, IMyInterface<T> where T : Enum
{
        public MyClass(Args args)
            : base(args)
        {
        }
}

public sealed class MyClass<T> : MyBaseClass, IMyInterface<T> where T : Enum
{
        public MyClass(Args args)
            : base(args)
        {
            var t = typeof(T);
            if (t.Namespace.IsNotContaining("My.Namespace.Of.Interest"))
            {
#pragma warning disable CA1303 // Do not pass literals as localized parameters
                throw new InvalidEnumArgumentException("The enum provided was not of the expected type.");
#pragma warning restore CA1303 // Do not pass literals as localized parameters
            }
        }
}