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. Convert string to property name?

Convert string to property name?

Scheduled Pinned Locked Moved C#
question
7 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.
  • B Offline
    B Offline
    BDJones
    wrote on last edited by
    #1

    I have an object named 'dv' 'dv' has a string property named 'refNumber' There is also a string 'altString' = "refNumber" Is there a way to use 'altString' to get that property from 'dv'? string newString = dv.??altString?? -- should set newString the same as -- string newString = dv.refNumber Thanks for your suggestions.

    N P 2 Replies Last reply
    0
    • B BDJones

      I have an object named 'dv' 'dv' has a string property named 'refNumber' There is also a string 'altString' = "refNumber" Is there a way to use 'altString' to get that property from 'dv'? string newString = dv.??altString?? -- should set newString the same as -- string newString = dv.refNumber Thanks for your suggestions.

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Reflection can do this. Use System.Reflection name space. You'd do something like (not tested)

      PropertyInfo property = (PropertyInfo) dv.GetMembers().SingleOrDefault(info =>
      info.MemberType == MemberTypes.Property &&
      info.Name == altString);
      string newString = string.Empty;
      if (property != null)
      newString = (string) property.GetValue(dv, null);

      :)

      Best wishes, Navaneeth

      B 1 Reply Last reply
      0
      • N N a v a n e e t h

        Reflection can do this. Use System.Reflection name space. You'd do something like (not tested)

        PropertyInfo property = (PropertyInfo) dv.GetMembers().SingleOrDefault(info =>
        info.MemberType == MemberTypes.Property &&
        info.Name == altString);
        string newString = string.Empty;
        if (property != null)
        newString = (string) property.GetValue(dv, null);

        :)

        Best wishes, Navaneeth

        B Offline
        B Offline
        BDJones
        wrote on last edited by
        #3

        Thank you. Please forgive my ignorance, but where does 'refNumber' occur in your reply?

        N 1 Reply Last reply
        0
        • B BDJones

          Thank you. Please forgive my ignorance, but where does 'refNumber' occur in your reply?

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          BDJones wrote:

          but where does 'refNumber' occur in your reply? Quote Selected Text

          You said it will be in altString variable. I am using that variable to compare against name. You can replace it with "refNumber" if you wish to.

          Best wishes, Navaneeth

          B 1 Reply Last reply
          0
          • B BDJones

            I have an object named 'dv' 'dv' has a string property named 'refNumber' There is also a string 'altString' = "refNumber" Is there a way to use 'altString' to get that property from 'dv'? string newString = dv.??altString?? -- should set newString the same as -- string newString = dv.refNumber Thanks for your suggestions.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            See this recent thread.[^]

            B 1 Reply Last reply
            0
            • N N a v a n e e t h

              BDJones wrote:

              but where does 'refNumber' occur in your reply? Quote Selected Text

              You said it will be in altString variable. I am using that variable to compare against name. You can replace it with "refNumber" if you wish to.

              Best wishes, Navaneeth

              B Offline
              B Offline
              BDJones
              wrote on last edited by
              #6

              Thank you.

              1 Reply Last reply
              0
              • P PIEBALDconsult

                See this recent thread.[^]

                B Offline
                B Offline
                BDJones
                wrote on last edited by
                #7

                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