C# 或 VB.NET - 迭代所有公共枚举

发布于 2024-10-03 15:29:22 字数 282 浏览 2 评论 0原文

我们的源代码中有一个通用组件,其中包含一个非常大的应用程序的所有枚举(大约 300 个!)。

有没有什么方法可以使用 C# 或 VB.NET 来遍历所有这些,以便对每一个执行操作?

问题如何迭代所有“公共字符串” .net 类中的属性 几乎是相关的,但我正在处理的枚举是多种类型的混合

We have a common component in our source which contains all the enums (approx 300!) for a very large application.

Is there any way, using either C# or VB.NET, to iterate through all of them in order to perform an action on each one?

The question How to iterate all “public string” properties in a .net class is almost relevant but the enums I am dealing with are a mix of types

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一杯敬自由 2024-10-10 15:29:22

有类似的东西吗?

var query = Assembly.GetExecutingAssembly()
                    .GetTypes()
                    .Where(t => t.IsEnum && t.IsPublic);

foreach (Type t in query)
{
    Console.WriteLine(t);
}

Something along these lines?

var query = Assembly.GetExecutingAssembly()
                    .GetTypes()
                    .Where(t => t.IsEnum && t.IsPublic);

foreach (Type t in query)
{
    Console.WriteLine(t);
}
梦忆晨望 2024-10-10 15:29:22

这应该可以帮助您开始。迭代加载的程序集中的类型;并检查它们是否是枚举:

List<string> enumValues = new List<string>();
foreach(Type t in AppDomain.CurrentDomain.GetAssemblies().Select(a=>a.GetTypes().Where(t=>t.IsEnum)))
{
    enumValues.AddRange(Enum.GetNames(t));
}

This should get you started. Iterate through the types in the loaded assemblies; and check whether they are an enum:

List<string> enumValues = new List<string>();
foreach(Type t in AppDomain.CurrentDomain.GetAssemblies().Select(a=>a.GetTypes().Where(t=>t.IsEnum)))
{
    enumValues.AddRange(Enum.GetNames(t));
}
赠意 2024-10-10 15:29:22

加载所有程序集后,您可以迭代每个程序集并调用 GetTypes() 以返回所有类型(您也可以选择获取私有类型,尽管这会花费更长的时间)。您可以按 IsEnum 过滤类型以仅获取枚举类型。请注意,这还将返回所有 BCL Enum 类型。如果您不需要 BCL 类型,则需要将其过滤掉。您可以通过忽略名称以“System”开头的程序集来摆脱其中的大多数。或者,您可以检查该路径是否包含您的本地路径,假设您的所有项目程序集都位于 EXE 本地。

Once all of your assemblies are loaded, you can iterate through each Assembly and call GetTypes() to return all types (you can optionally get private types as well, though this will take longer). You can filter the types by IsEnum to get only those that are enum types. Note that this will also return all BCL Enum types. If you don't want the BCL types, you'll need to filter them out. You can get rid of most of them by ignoring assemblies whose names start with "System". Alternately, you can check that the path contains your local path, assuming all of your project assemblies are local to your EXE.

小ぇ时光︴ 2024-10-10 15:29:22

如果您将所有enum编译成一个通用的enum,您可以使用类似以下内容的内容:

using System;

namespace SomeApp
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach(MyEnum target in Enum.GetValues(typeof(MyEnum)))
            {
                Console.WriteLine(target.ToString());
                // You can obviously perform an action on each one here.
            }
        }
    }

    enum MyEnum
    {
        One,
        Two,
        Three,
        Four,
        Five
    };
}

/*
Prints...

One
Two
Three
Four
Five
*/

希望这可以让您走上正确的道路。

If you have all the enums compiled into a common enum you could use something along the lines of this:

using System;

namespace SomeApp
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach(MyEnum target in Enum.GetValues(typeof(MyEnum)))
            {
                Console.WriteLine(target.ToString());
                // You can obviously perform an action on each one here.
            }
        }
    }

    enum MyEnum
    {
        One,
        Two,
        Three,
        Four,
        Five
    };
}

/*
Prints...

One
Two
Three
Four
Five
*/

Hopefully this can get you started down the right path.

雾里花 2024-10-10 15:29:22

假设您有枚举所在的程序集。

IEnumerable<Type> enums = assembly.GetTypes().Where(t => t.IsEmum);

Assuming you have the assembly on which the enums resides.

IEnumerable<Type> enums = assembly.GetTypes().Where(t => t.IsEmum);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文