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?
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?
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).
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.
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...