System.Reflection.PropertyInfo.SetValue () chamando manipulador de eventos padrão do botão [fechado]

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

  •  08-07-2019
  •  | 
  •  

Pergunta

Então, eu realmente não estou certo porque isso está acontecendo, mas eu estou correndo em alguns DataRows onde eu tenho o nome do controle, propriedade e valor que eu quero set. Tudo funciona bem, exceto quando eu definir a propriedade de texto de um botão. Por alguma razão, o evento clique é chamado ...

Aqui está parte do código que eu tenho:

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

Então, por que no mundo seria chamar o manipulador de eventos ao definir o valor? Aqui está o que eu estou definindo-o com que está causando isso:

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

Solução

Eu não posso reproduzir isso com um simples curto, mas programa 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);
    }
}

Você pode vir acima com um programa semelhante completo que não demonstrá-lo?

Outras dicas

Infelizmente, não, eu não poderia reproduzi-lo também. Eu não estou certo o que estava causando isso, mas tudo o que fiz para corrigi-lo era excluir o botão e colocá-lo novamente lá.

Não é certo o que era, mas graças para o código.

Você não escreveu isso em .Net2.0 não é?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top