GetEnumerator Error with Custom Type

穿精又带淫゛_ 提交于 2019-12-13 08:59:47

问题


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

    }

回答1:


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



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/33149703/getenumerator-error-with-custom-type

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