C# 或 VB.NET - 迭代所有公共枚举
我们的源代码中有一个通用组件,其中包含一个非常大的应用程序的所有枚举(大约 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有类似的东西吗?
Something along these lines?
这应该可以帮助您开始。迭代加载的程序集中的类型;并检查它们是否是枚举:
This should get you started. Iterate through the types in the loaded assemblies; and check whether they are an enum:
加载所有程序集后,您可以迭代每个程序集并调用 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.
如果您将所有
enum
编译成一个通用的enum
,您可以使用类似以下内容的内容:希望这可以让您走上正确的道路。
If you have all the
enums
compiled into a commonenum
you could use something along the lines of this:Hopefully this can get you started down the right path.
假设您有枚举所在的程序集。
Assuming you have the assembly on which the enums resides.