System.Reflection.PropertyInfo.SetValue () llamando al controlador de eventos predeterminado del botón [cerrado]

StackOverflow https://stackoverflow.com/questions/309706

  •  08-07-2019
  •  | 
  •  

Pregunta

Así que no estoy realmente seguro de por qué sucede esto, pero estoy ejecutando algunas DataRows donde tengo el nombre de control, la propiedad y el valor que quiero establecer. Todo funciona bien, excepto cuando configuro la propiedad TEXTO de un botón. Por alguna razón, el evento click se llama ...

Aquí hay algunos de los códigos que tengo:

string controlName, value, property;
Control currentControl = null;
System.Reflection.PropertyInfo propertyInfo = null;

// run through all rows in the table and set the property
foreach (DataRow r in languageDataset.Tables[_parentForm.Name].Rows)
{
  controlName = r["ControlName"].ToString().ToUpper();
  value = r["Value"].ToString();
  property = r["Property"].ToString();

  // check all controls on the form
  foreach (Control c in formControls)
  {
    // only change it if its the right control
    if (c.Name.ToUpper() == controlName)
    {
      propertyInfo = c.GetType().GetProperty(property);

      if (propertyInfo != null)
        propertyInfo.SetValue(c, value, null);  ******Calls Event Handler?!?!******
      //

      currentControl = c;
      break;
    }
  }
}

Entonces, ¿por qué en el mundo llamaría al controlador de eventos al establecer el valor? Esto es lo que estoy configurando con eso que está causando esto:

<SnappletChangePassword>  
  <ControlName>buttonAcceptPassword</ControlName>
  <Property>Text</Property>  
  <Value>Accept</Value>
</SnappletChangePassword>
¿Fue útil?

Solución

No puedo reproducir esto con un programa simple pero completo:

using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

class Test
{
    static void Main()
    {
        Button goButton = new Button { 
            Text = "Go!",
            Location = new Point(5, 5)
        };

        Button targetButton = new Button {
            Text = "Target",
            Location = new Point(5, 50)
        };
        goButton.Click += (sender, args) => SetProperty(targetButton, "Text", "Changed");
        targetButton.Click += (sender, args) => MessageBox.Show("Target clicked!");

        Form f = new Form { Width = 200, Height = 120,
                Controls = { goButton, targetButton }
        };
        Application.Run(f);
    }

    private static void SetProperty(object target, string propertyName, object value)
    {
        PropertyInfo property = target.GetType().GetProperty(propertyName);
        property.SetValue(target, value, null);
    }
}

¿Puedes crear un programa igualmente completo que lo demuestre ?

Otros consejos

Lamentablemente, no, tampoco pude reproducirlo. No estoy seguro de qué lo estaba causando, pero todo lo que hice para solucionarlo fue eliminar el botón y volver a colocarlo allí.

no estoy seguro de qué era, pero gracias por el código.

No escribiste eso en .Net2.0, ¿verdad?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top