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. Web Development
  3. ASP.NET
  4. Setting property via Reflection

Setting property via Reflection

Scheduled Pinned Locked Moved ASP.NET
tutorialquestion
7 Posts 2 Posters 1 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.
  • S Offline
    S Offline
    szukuro
    wrote on last edited by
    #1

    I want set properties to a WebControl via Reflection. The corresponding code snippet is something like this:

    public void SetProperty(Control control, string propertyname, string propertyvalue)
    {
    Type type = control.GetType();
    System.Reflection.PropertyInfo pi = type.GetProperties(propertyname);
    object value = Convert.ChangeType(propertyvalue, pi.PropertyType);
    pi.SetValue(control, value, null);
    }

    It works for most cases, but for special types like Unit or Color (Width or ForeColor for example), I get an Exception. I made some ifs for a few types so that I convert/parse them specifically to the type needed. But this way the code is quite ugly and is not guaranteed to work all the time. Anyone else know a better solution?

    M 1 Reply Last reply
    0
    • S szukuro

      I want set properties to a WebControl via Reflection. The corresponding code snippet is something like this:

      public void SetProperty(Control control, string propertyname, string propertyvalue)
      {
      Type type = control.GetType();
      System.Reflection.PropertyInfo pi = type.GetProperties(propertyname);
      object value = Convert.ChangeType(propertyvalue, pi.PropertyType);
      pi.SetValue(control, value, null);
      }

      It works for most cases, but for special types like Unit or Color (Width or ForeColor for example), I get an Exception. I made some ifs for a few types so that I convert/parse them specifically to the type needed. But this way the code is quite ugly and is not guaranteed to work all the time. Anyone else know a better solution?

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      szukuro wrote:

      Anyone else know a better solution?

      As long as I I don't know what your context is, for example why you need reflection here, then IMO it's hard to say this is a good/bad choice or whether there is a better one.

      S 1 Reply Last reply
      0
      • M minhpc_bk

        szukuro wrote:

        Anyone else know a better solution?

        As long as I I don't know what your context is, for example why you need reflection here, then IMO it's hard to say this is a good/bad choice or whether there is a better one.

        S Offline
        S Offline
        szukuro
        wrote on last edited by
        #3

        It's for a hobby project, kind of a WYSIWYG editor, but with server-side features. At this state the user types in some markup like and gets a TextBox created on the same page (via Ajax).

        M 1 Reply Last reply
        0
        • S szukuro

          It's for a hobby project, kind of a WYSIWYG editor, but with server-side features. At this state the user types in some markup like and gets a TextBox created on the same page (via Ajax).

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          You might want to see the ParseControl[^] method.

          S 1 Reply Last reply
          0
          • M minhpc_bk

            You might want to see the ParseControl[^] method.

            S Offline
            S Offline
            szukuro
            wrote on last edited by
            #5

            Thanks for the link, I didn't know of this method. This way I think the whole thing will be much easier to write :). Also despite this, I think my question still remains in a more general sense: How do/Can you set properties independent of their types easily (= without if and/or switch) from string input? Though now it's not really an ASP.NET question anymore..

            M 1 Reply Last reply
            0
            • S szukuro

              Thanks for the link, I didn't know of this method. This way I think the whole thing will be much easier to write :). Also despite this, I think my question still remains in a more general sense: How do/Can you set properties independent of their types easily (= without if and/or switch) from string input? Though now it's not really an ASP.NET question anymore..

              M Offline
              M Offline
              minhpc_bk
              wrote on last edited by
              #6

              In this case, you should use the the type converter of the property to convert a string value, this way will work not just with the primitive types, but also with custom types. To get the TypeConverter[^], you can use the TypeDescriptor[^], your sample code is updated a bit like this:

              public void SetProperty(Control control, string propertyname, string propertyvalue)
              {
              Type type = control.GetType();
              System.Reflection.PropertyInfo pi = type.GetProperty(propertyname);

              TypeConverter converter = TypeDescriptor.GetConverter(pi.PropertyType);
              object value = converter.ConvertFromInvariantString(propertyvalue);
                          
              //object value = Convert.ChangeType(propertyvalue, pi.PropertyType);
                         
              pi.SetValue(control, value, null);
              

              }

              S 1 Reply Last reply
              0
              • M minhpc_bk

                In this case, you should use the the type converter of the property to convert a string value, this way will work not just with the primitive types, but also with custom types. To get the TypeConverter[^], you can use the TypeDescriptor[^], your sample code is updated a bit like this:

                public void SetProperty(Control control, string propertyname, string propertyvalue)
                {
                Type type = control.GetType();
                System.Reflection.PropertyInfo pi = type.GetProperty(propertyname);

                TypeConverter converter = TypeDescriptor.GetConverter(pi.PropertyType);
                object value = converter.ConvertFromInvariantString(propertyvalue);
                            
                //object value = Convert.ChangeType(propertyvalue, pi.PropertyType);
                           
                pi.SetValue(control, value, null);
                

                }

                S Offline
                S Offline
                szukuro
                wrote on last edited by
                #7

                Again thank you.

                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