System.Reflection.PropertyInfo.SetValue () chiamando il gestore eventi predefinito del pulsante [chiuso]

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

  •  08-07-2019
  •  | 
  •  

Domanda

Quindi non sono davvero sicuro del perché questo stia accadendo, ma sto scorrendo alcuni DataRows in cui ho il nome, la proprietà e il valore di controllo che voglio impostare. Tutto funziona bene tranne quando imposto la proprietà TEXT di un pulsante. Per qualche motivo, l'evento click si chiama ...

Ecco un po 'del codice che ho:

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

Quindi perché nel mondo dovrebbe chiamare il gestore eventi quando imposta il valore? Ecco cosa sto impostando con ciò che sta causando questo:

<SnappletChangePassword>  
  <ControlName>buttonAcceptPassword</ControlName>
  <Property>Text</Property>  
  <Value>Accept</Value>
</SnappletChangePassword>
È stato utile?

Soluzione

Non riesco a riprodurlo con un programma semplice ma 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);
    }
}

Puoi escogitare un programma altrettanto completo che lo dimostra ?

Altri suggerimenti

Purtroppo, non sono riuscito a riprodurlo neanche io. Non sono sicuro di cosa l'abbia causato, ma tutto ciò che ho fatto per risolverlo è stato eliminare il pulsante e rimetterlo lì.

non sono sicuro di cosa fosse, ma grazie per il codice.

Non l'hai scritto in .Net2.0 vero?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top