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. Reflection Assistance please!

Reflection Assistance please!

Scheduled Pinned Locked Moved C#
questioncsharphelptutorial
6 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.
  • K Offline
    K Offline
    Kasdoffe
    wrote on last edited by
    #1

    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!

    H 1 Reply Last reply
    0
    • K Kasdoffe

      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!

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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

      A K 2 Replies Last reply
      0
      • H Heath Stewart

        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

        A Offline
        A Offline
        Anonymous
        wrote on last edited by
        #3

        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).

        H 1 Reply Last reply
        0
        • H Heath Stewart

          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

          K Offline
          K Offline
          Kasdoffe
          wrote on last edited by
          #4

          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.

          K 1 Reply Last reply
          0
          • K Kasdoffe

            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.

            K Offline
            K Offline
            Kasdoffe
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            • A Anonymous

              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).

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              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

              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