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. dynamic assigning of class properties

dynamic assigning of class properties

Scheduled Pinned Locked Moved C#
questiondata-structures
4 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.
  • T Offline
    T Offline
    TyronX
    wrote on last edited by
    #1

    I have a text file which contains different properties of a class (like integers, doubles, integer-array (more numbers divided by commas),..) I want to read and assign. Well, i managed to read all the key=value Pairs in that textfile and the looping through all the properties of that class. Now I want to assign these values to their appropriate property of the class. But how can i convert now the values (which are logically strings) to the datatype they have to be? I need something like object Convert.FromString(string str) Or I compare the read in keys with the properties from the class so that I know to which Datatype the string has to be converted, but on this way I need a way to typecast dynamically.

    J S 2 Replies Last reply
    0
    • T TyronX

      I have a text file which contains different properties of a class (like integers, doubles, integer-array (more numbers divided by commas),..) I want to read and assign. Well, i managed to read all the key=value Pairs in that textfile and the looping through all the properties of that class. Now I want to assign these values to their appropriate property of the class. But how can i convert now the values (which are logically strings) to the datatype they have to be? I need something like object Convert.FromString(string str) Or I compare the read in keys with the properties from the class so that I know to which Datatype the string has to be converted, but on this way I need a way to typecast dynamically.

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      See the System.Convert class for dynamic casting and the System.Reflection namespace for reading the return type of properties.

      1 Reply Last reply
      0
      • T TyronX

        I have a text file which contains different properties of a class (like integers, doubles, integer-array (more numbers divided by commas),..) I want to read and assign. Well, i managed to read all the key=value Pairs in that textfile and the looping through all the properties of that class. Now I want to assign these values to their appropriate property of the class. But how can i convert now the values (which are logically strings) to the datatype they have to be? I need something like object Convert.FromString(string str) Or I compare the read in keys with the properties from the class so that I know to which Datatype the string has to be converted, but on this way I need a way to typecast dynamically.

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

        object System.Convert.ChangeType(object value, Type conversionType, IFormatProvider provider);

        T 1 Reply Last reply
        0
        • S Skynyrd

          object System.Convert.ChangeType(object value, Type conversionType, IFormatProvider provider);

          T Offline
          T Offline
          TyronX
          wrote on last edited by
          #4

          that's what i was searching for :) thanks

          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