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. .NET (Core and Framework)
  4. Reflection Question

Reflection Question

Scheduled Pinned Locked Moved .NET (Core and Framework)
helpdatabasedata-structuresjsonquestion
3 Posts 3 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 am in dire need of some help. I have a project that I need to complete STAT! Here is the rundown: I have a form. This form has six controls on it. They do nothing. I have another form that retrieves these these control in a Control[] array. On this second form, there is a comboBox and a richTextBox. The comboBox is populated with the names of the controls. I have absolutely no problem getting the names of the properties; I can get them fine. I cannot get the value of each of these properties. I know that there is a GetValue(object, object[] index) method that is supposed to return the value of a property, but I can not get it to work to save my life!:confused: I have included the code from the SelectedIndexChanged event of the combobox, and the class that represents a property below. Assume that the rest of the program works because it does. If the GetValue call is dropped off the end of the last line below, all of the properites' names are properly listed in the textBox. Please show me how I should call this method. Any sugestions would be greatly appreciated!:cool: private void myCboBox_SelectedIndexChanged(object sender, EventArgs e) { //creates an int to hold the selected index: int index = myCboBox.SelectedIndex; //creates a type variable: Type t = controls[index].GetType(); MyProperty Myproperty = new MyProperty(t.Name, null); PropertyInfo[] myProps = t.GetProperties(); foreach (PropertyInfo p in myProps) { richTextBox1.Text += "\n" + //The GetValue call is whats killing me! //Throws a TargetException //Going crazy here! Please help! p.Name + "\t\t\t\t" +p.GetValue((object)Myproperty, null); } } } public class MyProperty { public MyProperty(string aName, object aValue) { theName = aName; theValue = aValue; } private static string theName; public static string theNameProp { get { return theName; } set { theName = value; } } private static object theValue; public static object intTest2 { get { return theValue; } set { theValue = value; } } } "If you don't know w

    U F 2 Replies Last reply
    0
    • M MrColeyted

      Hello all. I am in dire need of some help. I have a project that I need to complete STAT! Here is the rundown: I have a form. This form has six controls on it. They do nothing. I have another form that retrieves these these control in a Control[] array. On this second form, there is a comboBox and a richTextBox. The comboBox is populated with the names of the controls. I have absolutely no problem getting the names of the properties; I can get them fine. I cannot get the value of each of these properties. I know that there is a GetValue(object, object[] index) method that is supposed to return the value of a property, but I can not get it to work to save my life!:confused: I have included the code from the SelectedIndexChanged event of the combobox, and the class that represents a property below. Assume that the rest of the program works because it does. If the GetValue call is dropped off the end of the last line below, all of the properites' names are properly listed in the textBox. Please show me how I should call this method. Any sugestions would be greatly appreciated!:cool: private void myCboBox_SelectedIndexChanged(object sender, EventArgs e) { //creates an int to hold the selected index: int index = myCboBox.SelectedIndex; //creates a type variable: Type t = controls[index].GetType(); MyProperty Myproperty = new MyProperty(t.Name, null); PropertyInfo[] myProps = t.GetProperties(); foreach (PropertyInfo p in myProps) { richTextBox1.Text += "\n" + //The GetValue call is whats killing me! //Throws a TargetException //Going crazy here! Please help! p.Name + "\t\t\t\t" +p.GetValue((object)Myproperty, null); } } } public class MyProperty { public MyProperty(string aName, object aValue) { theName = aName; theValue = aValue; } private static string theName; public static string theNameProp { get { return theName; } set { theName = value; } } private static object theValue; public static object intTest2 { get { return theValue; } set { theValue = value; } } } "If you don't know w

      U Offline
      U Offline
      Urs Enzler
      wrote on last edited by
      #2

      In your foreach loop: 1) p is a PropertyInfo of Type t 2) you call p.GetValue with an instance of type MyProperty, which is probably not of type t Therefore the exception. You should call GetProperty with the instance from the controls collection, where you get the type from. regards Urs

      -^-^-^-^-^-^-^- no risk no funk

      1 Reply Last reply
      0
      • M MrColeyted

        Hello all. I am in dire need of some help. I have a project that I need to complete STAT! Here is the rundown: I have a form. This form has six controls on it. They do nothing. I have another form that retrieves these these control in a Control[] array. On this second form, there is a comboBox and a richTextBox. The comboBox is populated with the names of the controls. I have absolutely no problem getting the names of the properties; I can get them fine. I cannot get the value of each of these properties. I know that there is a GetValue(object, object[] index) method that is supposed to return the value of a property, but I can not get it to work to save my life!:confused: I have included the code from the SelectedIndexChanged event of the combobox, and the class that represents a property below. Assume that the rest of the program works because it does. If the GetValue call is dropped off the end of the last line below, all of the properites' names are properly listed in the textBox. Please show me how I should call this method. Any sugestions would be greatly appreciated!:cool: private void myCboBox_SelectedIndexChanged(object sender, EventArgs e) { //creates an int to hold the selected index: int index = myCboBox.SelectedIndex; //creates a type variable: Type t = controls[index].GetType(); MyProperty Myproperty = new MyProperty(t.Name, null); PropertyInfo[] myProps = t.GetProperties(); foreach (PropertyInfo p in myProps) { richTextBox1.Text += "\n" + //The GetValue call is whats killing me! //Throws a TargetException //Going crazy here! Please help! p.Name + "\t\t\t\t" +p.GetValue((object)Myproperty, null); } } } public class MyProperty { public MyProperty(string aName, object aValue) { theName = aName; theValue = aValue; } private static string theName; public static string theNameProp { get { return theName; } set { theName = value; } } private static object theValue; public static object intTest2 { get { return theValue; } set { theValue = value; } } } "If you don't know w

        F Offline
        F Offline
        Fayu
        wrote on last edited by
        #3

        Try p.GetValue(Myproperty, null) instead of p.GetValue((object)Myproperty, null). The is no need to cast Myproperty to an object since its already an object.

        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