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. Expando object - creating a dynamic class and reading the property types in a unit test

Expando object - creating a dynamic class and reading the property types in a unit test

Scheduled Pinned Locked Moved C#
testingdebugginghelpquestionlearning
4 Posts 2 Posters 2 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
    Simon_Whale
    wrote on last edited by
    #1

    dynamic returnObject = new ExpandoObject() as IDictionary;

    // Other code actions here

    var EnterData = (IDictionary)returnObject

    EnterData.Add("myColumn", typeof(int));

    Now when I debug a test that I have created I can see the value type has been set to int. but I would like to add a test to this, purely for my own learning

    var keypair = EnterData.FirstOrDefault(p=> p.Key == "myColumn");
    var properties = keypair.GetType().GetGenericArguments();
    var ValueType = properties[1];

    which always returns a type object, is there a way to get the type that I have assigned to the keyvaluepair?

    Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

    L 1 Reply Last reply
    0
    • S Simon_Whale

      dynamic returnObject = new ExpandoObject() as IDictionary;

      // Other code actions here

      var EnterData = (IDictionary)returnObject

      EnterData.Add("myColumn", typeof(int));

      Now when I debug a test that I have created I can see the value type has been set to int. but I would like to add a test to this, purely for my own learning

      var keypair = EnterData.FirstOrDefault(p=> p.Key == "myColumn");
      var properties = keypair.GetType().GetGenericArguments();
      var ValueType = properties[1];

      which always returns a type object, is there a way to get the type that I have assigned to the keyvaluepair?

      Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Maybe I'm doing or reading it wrong, but wouldn't that be

              string myValueType = keypair.Value.ToString();
      

      That should return "System.Int32", and the first part of the pair is always a string.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

      S 1 Reply Last reply
      0
      • L Lost User

        Maybe I'm doing or reading it wrong, but wouldn't that be

                string myValueType = keypair.Value.ToString();
        

        That should return "System.Int32", and the first part of the pair is always a string.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

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

        Thanks Eddie, it worked.. Silly me, while looking my brain thought everything must complicated so I ignored all the easy stuff. :doh:

        Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

        L 1 Reply Last reply
        0
        • S Simon_Whale

          Thanks Eddie, it worked.. Silly me, while looking my brain thought everything must complicated so I ignored all the easy stuff. :doh:

          Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You're welcome, and happens to us all :D

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          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