问题循环Enum类值 [英] Problem cycling Enum class values

查看:88
本文介绍了问题循环Enum类值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个语义Web应用程序,其中使用了本体的组装.我使用Rowlex OWLGrinder将OWL转换为程序集.

I'm working on a semantic web application in which assembly of an ontology is beeing used. I used Rowlex OWLGrinder for converting OWL to assembly.

在本体中,有一些类具有个体,这些类是经过转换的tp Enum类,其中包含.dll程序集中的某些常量.例如,一个名为Language的OWL类和一个单独的名为English的类将被转换为一个名为Language的类,其中包含英语常量. Language.English是一个字符串,包含为本体中的个人指定的URI.

In the ontology there are some classes having individuals, which are converted tp Enum classes containing some constants in .dll assemblies. For example an OWL class named Language with an individual named English, will be converted to a class named Language containing English constant. The Language.English is a string, containing the URI specified for the individual in the ontology.

替代文字http://img5.imageshack.us/img5/9308/73263054 .jpg 替代文字http://img5.imageshack.us/img5/2246/11461238.jpg

在这种情况下,我找不到在枚举类常量之间循环的方法.例如,使用类似这样的内容:

I this context I can not find a way to cycle between enum class constants. For example using something like this:

    foreach (string item in Enum.GetNames(typeof(Language)))
    {

    }

此代码引发异常,表明Language不是枚举.

this code throws an exception saying that Language isn't an Enum.

我想知道是否有人会帮助我解决这个问题.

I was wondering if anyone would help me in this problem.

推荐答案

如错误所示,它不是真正的枚举.

As the error says, it's not a real enum.

听起来您需要反思:

var fields = typeof(Language).GetFields(BindingFlags.Static 
                                        | BindingFlags.Public);
foreach (string item in fields.Select(field => field.GetValue(null)))
{
     // ...
}

假设类型中没有其他公共静态字段.您总是可以按类型等进行过滤.

That's assuming there are no other public static fields in the type. You could always filter by type etc.

这篇关于问题循环Enum类值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆