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. Getting data from an array of objects

Getting data from an array of objects

Scheduled Pinned Locked Moved C#
questiondata-structureshelp
3 Posts 2 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.
  • M Offline
    M Offline
    ma poggi cons
    wrote on last edited by
    #1

    I need a way to get the values of fields from an array of classes, passed as an object to a method.

    myClass[] mc = new myClass[10];
    // set properties, fields, etc.
    myMethod(mc);

    where myMethod is defined as follows.

    public void myMethod(object obj) {
    Type t = obj.GetType();
    if (t.IsArray) {
    // how can I get the values of fields
    // for each element of the array ?
    }
    else {
    foreach (FieldInfo fi in t.GetFields()) {
    Type fieldType = fi.FieldType;
    // do something
    }
    }
    }

    Thank you for any help.

    U 1 Reply Last reply
    0
    • M ma poggi cons

      I need a way to get the values of fields from an array of classes, passed as an object to a method.

      myClass[] mc = new myClass[10];
      // set properties, fields, etc.
      myMethod(mc);

      where myMethod is defined as follows.

      public void myMethod(object obj) {
      Type t = obj.GetType();
      if (t.IsArray) {
      // how can I get the values of fields
      // for each element of the array ?
      }
      else {
      foreach (FieldInfo fi in t.GetFields()) {
      Type fieldType = fi.FieldType;
      // do something
      }
      }
      }

      Thank you for any help.

      U Offline
      U Offline
      Utku KAYA
      wrote on last edited by
      #2

      Use System.Reflection; MemberInfo[] mi = t[i].GetType().GetMembers(); MemberInfo type gives informtion about members

      M 1 Reply Last reply
      0
      • U Utku KAYA

        Use System.Reflection; MemberInfo[] mi = t[i].GetType().GetMembers(); MemberInfo type gives informtion about members

        M Offline
        M Offline
        ma poggi cons
        wrote on last edited by
        #3

        Thank you, but I have tried that already. It does not compile, returning the error "cannot apply indexing [] to an expression of type 'System.Type' (or something like that) for "t[i]".

        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