自定义类型的GetEnumerator错误 [英] GetEnumerator Error with Custom Type

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

问题描述

我有以下课程...

class gridRecord
{
    //Constructor
    public gridRecord()
    {
        Quantity = new quantityField();
        Title = new titleField();
        Pages = new pagesField();
    }
    private quantityField quantity;
    private titleField title;
    private pagesField pages;



    internal quantityField Quantity
    {
        get { return quantity; }
        set { quantity = value; }
    }

    internal titleField Title
    {
        get { return title; }
        set { title = value; }
    }

    internal pagesField Pages
    {
        get { return pages; }
        set { pages = value; }
    }
}

我希望能够以字符串的形式获取每个字段的名称,以便以后无需指定每一列就可以创建数据集.

I want to be able to get the name of each field as a string so I can later create a datable with out having to specify each column.

List<gridRecord> lgr = new List<gridRecord>();
lgr = populatedList();

foreach(gridField gf in lgr[0])
    MessageBox.Show(gf.ToString());

但是我得到这个错误:

错误1 foreach语句无法对类型为变量的变量进行操作 "XML__Console.gridRecord",因为"XML__Console.gridRecord"没有 包含"GetEnumerator"的公共定义

Error 1 foreach statement cannot operate on variables of type 'XML__Console.gridRecord' because 'XML__Console.gridRecord' does not contain a public definition for 'GetEnumerator'

我假设我需要继承表单和接口等,但是不确定如何或继承哪个.

I assume I need to inherit form and interface or something but not sure how or which one.

已添加网格字段...

Grid Field Added...

class gridField : Validateit
{
    public gridField()
    {
        Value = "---";
        isValid = false;
        message = "";
    }
    private string value;
    protected bool isValid;
    private string message;

    public string Value
    {
        get { return this.value; }
        set { this.value = value; }
    }
    public bool IsValid
    {
        get { return isValid; }
        set { isValid = value; }
    }
    public string Message
    {
        get { return message; }
        set { message = value; }
    }
    public override void Validate()
    {

    }
}

在其他字段下面添加的

quantityField基本上相同

quantityField added below the other fields are much the same

class quantityField : gridField
    {

        public void validate()
        {            
            if (isQuantityValid(Value) == false) { Value = "Invalid";}           
        }

        public static bool isQuantityValid(string quantity)
        {

            if (quantity.Length > 3)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }

推荐答案

根据我的理解,您想从gridRecord类中获取属性的名称(例如,您的示例:"Quantity","Title", 页面")?

From what i understood, you want to get the name of the properties from the gridRecord class (from your example: "Quantity", "Title", "Pages")?

为此,您需要使用Reflection:

For that, you need to use Reflection:

var properties = typeof(gridRecord).GetProperties();
foreach (PropertyInfo propInfo in properties) {
    MessageBox.Show(propInfo.Name);
}

这篇关于自定义类型的GetEnumerator错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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