Is it possible to get the property of a class from string and then set a value?
Example:
string s = "label1.text";
string value = "new value";
label1.text = value; <--and some code that makes this
How to do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If the given control is an instance variable on your form (if you used the built-in WinForms designer, most are), first get the control, and then set the property on it:
You may need to tweak
BindingFlagsto get this to work.This must be a method on your form. Call it as:
SetControlProperty(“myLabel”, “Text”, “my label text”);
Pay attention to the scope of the method. It any control within the form, but not the form itself (to access the form itself, set
controltothis).Note that this uses reflection and will be slow and brittle (change the name of a control and it will break).