Reflection Assistance please!
-
Very simple example of what I want to do. I have a textbox on my form: TextBox textBox1 = new TextBox(); I have a string containing the name of my textbox: string strControlName = "textBox1"; I have a string that contains the text for my textbox to display: string strControlText = "Hello World"; How do i use strControlName and strControlText to set my textbox's text property? (without looping through my controls on the form and using if statements). In another language I know, you can use 'Indirection'. It's a very useful and powerful tool. This acts similar to the following: set (@strControlName).Text = strControlText where the end result is textBox1.Text = "Hello World". I would love to be able to do this in C#! I was told to read up on reflection, but couldn't find what I wanted. Is there somehow a way to modify an existing control when all you know is the control name? (without looping through all the controls) Please help!
-
Very simple example of what I want to do. I have a textbox on my form: TextBox textBox1 = new TextBox(); I have a string containing the name of my textbox: string strControlName = "textBox1"; I have a string that contains the text for my textbox to display: string strControlText = "Hello World"; How do i use strControlName and strControlText to set my textbox's text property? (without looping through my controls on the form and using if statements). In another language I know, you can use 'Indirection'. It's a very useful and powerful tool. This acts similar to the following: set (@strControlName).Text = strControlText where the end result is textBox1.Text = "Hello World". I would love to be able to do this in C#! I was told to read up on reflection, but couldn't find what I wanted. Is there somehow a way to modify an existing control when all you know is the control name? (without looping through all the controls) Please help!
It depends - is the
TextBox
a field or property declared in your container (i.e.,Form
,UserControl
, etc.) or is it simply a variable? Reflection only works on type metadata, so if it's a variable it won't work. If it is a field, then you could do something like this:public void SetText(string name, string text)
{
FieldInfo field = this.GetType().GetField(name);
if (field != null)
{
PropertyInfo prop = field.FieldType.GetProperty("Text");
if (prop != null && prop.CanWrite)
{
object obj = field.GetValue(this);
prop.SetValue(obj, text, null);
}
}
}This also works best when you don't specify the
Name
properties of controls, which isn't required (the designer does, though).Microsoft MVP, Visual C# My Articles
-
It depends - is the
TextBox
a field or property declared in your container (i.e.,Form
,UserControl
, etc.) or is it simply a variable? Reflection only works on type metadata, so if it's a variable it won't work. If it is a field, then you could do something like this:public void SetText(string name, string text)
{
FieldInfo field = this.GetType().GetField(name);
if (field != null)
{
PropertyInfo prop = field.FieldType.GetProperty("Text");
if (prop != null && prop.CanWrite)
{
object obj = field.GetValue(this);
prop.SetValue(obj, text, null);
}
}
}This also works best when you don't specify the
Name
properties of controls, which isn't required (the designer does, though).Microsoft MVP, Visual C# My Articles
-
It depends - is the
TextBox
a field or property declared in your container (i.e.,Form
,UserControl
, etc.) or is it simply a variable? Reflection only works on type metadata, so if it's a variable it won't work. If it is a field, then you could do something like this:public void SetText(string name, string text)
{
FieldInfo field = this.GetType().GetField(name);
if (field != null)
{
PropertyInfo prop = field.FieldType.GetProperty("Text");
if (prop != null && prop.CanWrite)
{
object obj = field.GetValue(this);
prop.SetValue(obj, text, null);
}
}
}This also works best when you don't specify the
Name
properties of controls, which isn't required (the designer does, though).Microsoft MVP, Visual C# My Articles
-
I think I understand what you're doing here, except for the last 2 lines. I don't have the object reference for my textbox. All i have is the name of the textbox. I need to be able to get this reference and set the text property on my textbox control.
Kasdoffe wrote: I think I understand what you're doing here, except for the last 2 lines Nevermind, I'm dumb :) Anyways I got it to work like you showed except I had to add BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public as the second parameter to the GetField() method. Thanks for your help! This is awesome!
-
the TextBox is an actual control that I placed on my form. Does that help? I tried your code, but field is returned as null when I do the this.GetType().GetField(name).
That should be:
FieldInfo field this.GetType().GetField(name,
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);This will include non-public (i.e., private or protected) members in the search as well.
Microsoft MVP, Visual C# My Articles