Question

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?

Was it helpful?

Solution

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:

    void Form_SetControlProperty(
        String controlName, String propertyName, object value)
    {
        FieldInfo controlField = this.GetType().GetField(controlName, 
            BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
        object control = controlField.GetValue(this);
        PropertyInfo property = control.GetType().GetProperty(propertyName);
        property.SetValue(control, value, new object[0]);
    }

You may need to tweak BindingFlags to 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 control to this).

Note that this uses reflection and will be slow and brittle (change the name of a control and it will break).

OTHER TIPS

Based this source, the equivalent of

shipment.<propName> = valueToUse,

where 'propName' is the name of the property provided as a string:

using System;
using System.Reflection;

namespace PropertyViaString
{
    public class Shipment
    {
        public string Sender { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Shipment shipment = new Shipment();
            SetValueExample(shipment, "Sender", "Popeye");
            Console.WriteLine("Sender is {0}", shipment.Sender);
            Console.ReadKey();
        }

        static void  SetValueExample(Shipment shipment, string propName, string valueToUse)
        {
            Type type = shipment.GetType();
            PropertyInfo senderProperty = type.GetProperty(propName);
            senderProperty.SetValue(shipment, valueToUse, null);
        }

    }
}

prints

Sender is Popeye

You can use reflection to do this, but it will be quite slow?

Perhaps if you tell us what you're trying to achieve by doing this we can help, there are several patterns on event handlers etc. that usually makes this unnecessary.

The answer is use Reflection. However, there are many app frameworks that make the process much easier.

For example, have a look at Spring.Net Expressions. It allows you to do:

ExpressionEvaluator.SetValue(object, "label1", "text");

It is much more powerful and flexible than this simple example, so have a look.

You need an instance of the object whose properties you want to set. From your example I'll pretend it is a label.

Label myLabel = new Label();
string s = "text";
string value = "new value";
System.Reflection.PropertyInfo[] properties = myLabel.GetType().GetProperties();
foreach (System.Reflection.PropertyInfo p in properties) 
{
    if(p.Name == s)
    {
         p.SetValue(myLabel, value, null);
    }
}

I found this code:

Object someObject = myForm; <--- want to make this Object someObject = "myForm";
String propName = "Title";
System.Reflection.PropertyInfo pi = someObject.GetType().GetProperty(propName);
pi.SetValue(someObject, "New Value", new Object[0]);

It works. But what to do, that it would by possible to set someObject as a string.

Object someObject = (object)"myForm" <-- this doesn't work.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top