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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Reflection with a List

Reflection with a List

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

    I'm trying to find a cleaner way to reflectively get at the contents of my class that are lists of other classes:

    class BigClass
    {
    public List<LittleClass1> LittleList1;
    public List<LittleClass2> LittleList2;
    public string BigString;
    }

    class LittleClass1
    {
    public string a;
    public int b;
    }

    class LittleClass2 : LittleClass1
    {
    public string c;
    }

    To look into BigClass I use

    foreach (FieldInfo fi in BigClass.GetFields())
    {
    if (fi.GetValue(bigClassInstance).ToString().StartsWith("System.Collections.Generic.List"))
    {
    //must be a list
    }
    }

    It finds the lists, and I can process them from there, but there must be a better way.

    P 1 Reply Last reply
    0
    • M MollyTheCoder

      I'm trying to find a cleaner way to reflectively get at the contents of my class that are lists of other classes:

      class BigClass
      {
      public List<LittleClass1> LittleList1;
      public List<LittleClass2> LittleList2;
      public string BigString;
      }

      class LittleClass1
      {
      public string a;
      public int b;
      }

      class LittleClass2 : LittleClass1
      {
      public string c;
      }

      To look into BigClass I use

      foreach (FieldInfo fi in BigClass.GetFields())
      {
      if (fi.GetValue(bigClassInstance).ToString().StartsWith("System.Collections.Generic.List"))
      {
      //must be a list
      }
      }

      It finds the lists, and I can process them from there, but there must be a better way.

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

      Try the is and as operators rather than string comparisons. And probably look for IEnumerable rather than List.

      M 1 Reply Last reply
      0
      • P PIEBALDconsult

        Try the is and as operators rather than string comparisons. And probably look for IEnumerable rather than List.

        M Offline
        M Offline
        MollyTheCoder
        wrote on last edited by
        #3

        Thank you. I'll put up a big sign on my desk with "is" and "as" and ToString() so I don't have to ask this again.

        P 1 Reply Last reply
        0
        • M MollyTheCoder

          Thank you. I'll put up a big sign on my desk with "is" and "as" and ToString() so I don't have to ask this again.

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

          Spread the word.

          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