Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. System.Reflection GetValue method

System.Reflection GetValue method

Scheduled Pinned Locked Moved C#
helptutorialdatabaseregex
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MrColeyted
    wrote on last edited by
    #1

    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

    H 1 Reply Last reply
    0
    • M MrColeyted

      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

      H Offline
      H Offline
      Hesham Amin
      wrote on last edited by
      #2

      When using GetValue, you must pass an object of the same type that you used to create the PropertyInfo 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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups