mercredi 24 avril 2019

C# reflection - set value in windows form button

I have a requirement where i have to create a 52 buttons and set a value to them. So that i am trying to use reflection to set a value to each buttons.

PropertyInfo propertyInfo = this.GetType().GetProperty("button1");
propertyInfo.SetValue(this, Convert.ChangeType("Nishan", propertyInfo.PropertyType), null);

This is how i have initialized my buttons

public System.Windows.Forms.Button button1 { get; set; }

But this throws an exception

System.InvalidCastException: 'Invalid cast from 'System.String' to 'System.Windows.Forms.Button'.'

NOTE

button1.Text = "Nishan";

I want to done this approach in reflection way.

Can i do that?





Aucun commentaire:

Enregistrer un commentaire