GetEnumerator Error with Custom Type

2019-09-19 23:35发布

I have the following Class...

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());

But I get this error:

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 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;
            }
        }

    }

3条回答
别忘想泡老子
2楼-- · 2019-09-19 23:56

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

For that, you need to use Reflection:

var properties = typeof(gridRecord).GetProperties();
foreach (PropertyInfo propInfo in properties) {
    MessageBox.Show(propInfo.Name);
}
查看更多
Viruses.
3楼-- · 2019-09-20 00:04

If you want to get name of each field you should use reflection.
PropertyInfo class should be helpful

查看更多
ゆ 、 Hurt°
4楼-- · 2019-09-20 00:08

You are attempting to iterate over one element of the collection.

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

If your goal is to retrieve the name of each property within the class, then do this.

PropertyInfo[] props = typeof(gridRecord).GetProperties();
foreach(PropertyInfo prop in props) 
{
    object[] attrs = prop.GetCustomAttributes(true);
    foreach(object attr in attrs) 
    {
        AuthorAttribute authAttr = attr as AuthorAttribute;
        if (authAttr != null) 
        {
            string propName = prop.Name;
            string auth = authAttr.Name;
        }
    }
}
查看更多
登录 后发表回答