选择枚举类的常量时出现问题 [英] Problem in selecting constants of an enum class

查看:207
本文介绍了选择枚举类的常量时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在.net(由Rowlex OwlGrinder从我的OWL类创建的)中枚举类的常量之间循环之间存在一个问题.通过使用.net反射解决了问题(感谢亲爱的Jon Skeet先生的大力帮助): stackoverflow:problem-cycling-enum-class-values

I've encourted a problem recently about cycling between constants of an enum class in .net (that is created from my OWL class, by Rowlex OwlGrinder). Problem was solved by means of using .net reflection (thanks to great help from dear Mr. Jon Skeet): stackoverflow:problem-cycling-enum-class-values

通过解决这个问题,我开始使用它.在将dropDownList选定值与枚举类实例之一匹配后,我不得不通过谓词(= hasLanguage)将选定对象(= Language)声明为我的RDF主题(= learningResource).

By solving this, I started using it. After matching a dropDownList selected value to one of the enum class instances, I had to declare the selected object(=Language) to my RDF subject(=learningResource), via a predicate (=hasLanguage).

//learningResource is a new RDF subject, hasLanguage is predicate, and there
        //is a new value for it - Language.

        System.Reflection.FieldInfo[] resLanFields =
            typeof(Language).GetFields();

        for (int i = 0; i < resLangFields.Length; i++)
        {
            if (resLanFields[i].Name.Equals(dropDownList_lang.SelectedValue))
                learningResource.hasLanguage = ??? //i-th constant of Language
        }

现在出现问题;我不能使用Language [i](或类似的方法来选择Language类的第i个常量)来分配给hasLanguage.有没有办法选择枚举类的第i个常量(如反射)? 在这种情况下,有人可以帮我吗?

Now the problem appears; I can not use Language[i] (or something like this to select i-th constant of Language class) to assign to hasLanguage. Is there a way to select i-th constant of an enum class (like refelections)? Would any one please help me in this situation?

推荐答案

Language类不是C#术语中的枚举.这是具有公共const字符串字段的普通类. ROWLEX出于两个原因有意生成枚举模仿类而不是本机枚举:

The Language class is not an enum in C# terminology. It is an ordinary class with public const string fields. ROWLEX intentionally generates enum-imitating-classes instead of native enums for 2 reasons:

  1. 本机C#枚举的值是一个整数,而公共const字符串字段可以使用OWL类实例的URI.
  2. 该类可以具有一个附加的公共静态字符串,即"URI",它对于每个ROWLEX生成的类始终表示该类URI.

那是背景.如果我对您的问题的理解正确,那么您就遇到了将下拉列表中显示的所选名称绑定回URI的问题,并且您想使用元素在创建的数组中的位置.我不会那样做.对于每个列表项,DropDownList通常都具有ID和Value字段(可以以不同的组件命名). ID应该是唯一的对象,而Value应该包含人类可读的内容.使用"enum"字段的URI和ID设置ID,就像完成操作一样.因此,这就是您填充下拉菜单的方式:

That was the background. If I understood your question right, you had an issue binding the selected name displayed in the dropdown back to the URI, and you wanted to use the position of the element inside the array you created. I would not do it that way. DropDownLists typically have both ID and Value fields for every list item (can be named differently component to component). The ID is expected to be a unique object while the Value is expected to contain something human readable. Set the ID with URI of the "enum" field, and the Value as you have done. So this is how you populate your dropdown:

System.Reflection.FieldInfo[] resLanFields = typeof(Language).GetFields();
foreach(FieldInfo field in resLanFields)
{
  ListItem item = new ListItem();
  item.ID = field.GetValue(null); // takes the URI value of the const field
  item.Value = field.Name; // takes the name of the const field
  dropDownList_lang.AddItem(item);
}

当用户做出选择时,这就是您读出结果的方式:

And when the user made his/her choice, this is how you read the result out:

learningResource.hasLanguage = (string) dropDownList_lang.SelectedItem.ID;

由于在ROWLEX枚举模仿类(字符串=>语言)上实现了隐式转换运算符,因此您可以安全地将三元主题设置为字符串.应该编译没有问题.很简单,不是吗? ;)

Since there is implicit casting operator implemented on the ROWLEX enum-imitating-class (string=>Language) you can safely set your triple subject as a string. Should compile without issue. Simple, isn't it? ;)

这篇关于选择枚举类的常量时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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