System.Reflection GetValue method
-
Hello all. I need some assistance. If I have a form that has buttons (or any Control(s)) on it, and I want to be able to populate a datagridview at runtime by selecting the control name from a combobox, how do you call the GetValue(object, object[] index) method on it to get the value of the property. I have no problem getting the list of property names, but I cannot get the values!:mad: This code works. All but the GetValue method. I keep getting a 'TargetException: Object does not match target type' error. Please give me a code example of how to properly call this GetValue(object, object[] index) method. Once again, The method that updates the gridview(right now it is a textbox in this code; I will change it later) and the class that handles the properties: private void myCboBox_SelectedIndexChanged(object sender, EventArgs e) { MyProperty Myproperty = new MyProperty(null, null); //creates an int to hold the selected index: int index = myCboBox.SelectedIndex; //creates a type variable: Type t = controls[index].GetType(); PropertyInfo[] myProps = t.GetProperties(); foreach (PropertyInfo p in myProps) { richTextBox1.Text += "\n" + //This GetValue(object, object[] index) //is what I need to know how to do: p.Name + "\t\t\t\t" + p.GetValue(Myproperty, null); } } } public class MyProperty { public MyProperty(string aName, object aValue) { theName = aName; theValue = aValue; } private string theName; public string theNameProp { get { return theName; } set { theName = value; } } private object theValue; public object intTest2 { get { return theValue; } set { theValue = value; } } } "If you don't know where you're going, you'll probably end up somewhere else." Yogi Berra
-
Hello all. I need some assistance. If I have a form that has buttons (or any Control(s)) on it, and I want to be able to populate a datagridview at runtime by selecting the control name from a combobox, how do you call the GetValue(object, object[] index) method on it to get the value of the property. I have no problem getting the list of property names, but I cannot get the values!:mad: This code works. All but the GetValue method. I keep getting a 'TargetException: Object does not match target type' error. Please give me a code example of how to properly call this GetValue(object, object[] index) method. Once again, The method that updates the gridview(right now it is a textbox in this code; I will change it later) and the class that handles the properties: private void myCboBox_SelectedIndexChanged(object sender, EventArgs e) { MyProperty Myproperty = new MyProperty(null, null); //creates an int to hold the selected index: int index = myCboBox.SelectedIndex; //creates a type variable: Type t = controls[index].GetType(); PropertyInfo[] myProps = t.GetProperties(); foreach (PropertyInfo p in myProps) { richTextBox1.Text += "\n" + //This GetValue(object, object[] index) //is what I need to know how to do: p.Name + "\t\t\t\t" + p.GetValue(Myproperty, null); } } } public class MyProperty { public MyProperty(string aName, object aValue) { theName = aName; theValue = aValue; } private string theName; public string theNameProp { get { return theName; } set { theName = value; } } private object theValue; public object intTest2 { get { return theValue; } set { theValue = value; } } } "If you don't know where you're going, you'll probably end up somewhere else." Yogi Berra
When using
GetValue
, you must pass an object of the same type that you used to create thePropertyInfo
object. So this should work:p.GetValue(controls[index], null);
Then you can fill the MyProperty object if you need to.Hesham A. Amin My blog