Problem cycling Enum class values

回眸只為那壹抹淺笑 提交于 2019-12-11 15:57:52

问题


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.

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.

alt text http://img5.imageshack.us/img5/9308/73263054.jpg alt text 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)))
    {

    }

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

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


回答1:


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

It sounds like you need reflection:

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.



来源:https://stackoverflow.com/questions/938828/problem-cycling-enum-class-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!