简体   繁体   中英

Set custom properties using reflection on custom UserControl

I need to use reflection to set two custom propertys of custom userControls

CustomUserControl.cs

......

public string _ValidaMsg { get { return _ValidarMsg; } set { _ValidarMsg = value; } }
public bool _Valida { get { return _Validar; } set { _Validar = value;} }

._Valida determinates if the control needs to be validated before submitting the form and ._ValidaMsg is the message it shows when the field isn't completed

In my form.cs I have a typed List called listaMetodo (String ControlName,String ControlText,Type Control.Type) Here's the method where I fill my list

 private List<xEntidades.entControlValidacion> GetAllControls(Control container)
    {
        foreach (Control c in container.Controls)
        {
            GetAllControls(c);
            // these txtDescripcion and others are the diferent types of my customs UserControls
            if (c.Name.Equals("txtDescripcion") || c.Name.Equals("Combo") || c.Name.Equals("txtCodigo") || c.Name.Equals("txtFecha"))
            {

                    listaMetodo.Add(new xEntidades.entControlValidacion(c.Parent.Name, c.Text, c.GetType()));

            }

        }

        return listaMetodo;
    }

So once I have the list with all controls that are relevant, what I need to do is to compare it with another that I have, wich is filled with all the controls that needs to be validated, let's call it validateList(string NameOfControl,bool Valida , String ValidaMsg) . In the for loop while im itherating the two list I have the condition

If(listaMetodo[iExample].ControlName.equals(validateList[jExample].NameOfControl && validateList[jExample].Valida){

//here I should get the instance of the object by reflection and change ._ValidaMsg and ._Valida

}

So, how can I Modify the propertys of the instanced object using the Type and the ControlName Stored in my listaMetodo It's mandatory to use reflection? Is there another approatch to achieve what I wat to? Also Im Working with Framework 2.0 Thanks in Advance

Well finally I achieved what i wanted creating this method

     private void validaControles()
        {
            for (int j = 0; j < listaMetodo.Count; j++)
            {
                for (int k = 0; k < listaControlesValida.Count; k++)
                {

//just ignore this condition


if(listaMetodo[j].NombrePadre.Equals(listaControlesValida[k].Control) && listaControlesValida[k].Valida && listaMetodo[j].TextoControl.Equals("")){

                   listaControlesValida[k].ValidaText, null);
                        Control[] control = this.Controls.Find(listaMetodo[j].NombrePadre, true);
                        Type type = control[0].GetType();

                       control[0].GetType().GetProperty("_ValidaMsg").SetValue(control[0], listaControlesValida[k].ValidaText, null);
                       control[0].GetType().GetProperty("_Valida").SetValue(control[0], true, null);
                    }
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM