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. Reflection Example

Reflection Example

Scheduled Pinned Locked Moved C#
tutorialquestion
5 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.
  • E Offline
    E Offline
    eggie5
    wrote on last edited by
    #1

    How can I get the collection of field values from the below class. So consider this instance of SentRecord SentRecord sr=new SentRecord(1,new Subscriber(), DataTime.Now, false, "C:\img.jpg", 0); Now how can I get all fields and values from sr. e.g.: SentRecordID => 1 ContentCampaign => [ContentCampaign object] Subscriber => [Subscriber object] TimeSent => [DateTime object] Confirmed => false FileSent => "C:\img.jpg" AdIDDelivered => 0

    public class SentRecord : Model
    {
    public SentRecord(int contentID, Subscriber subscriber, DateTime timeSent, bool confirmed, string fileSent, int adIDDelivered)
    {
    this.ContentCampaign.ContentID = contentID;
    this.Subscriber = subscriber;
    this.TimeSent = timeSent;
    this.Confirmed = confirmed;
    this.FileSent = fileSent;
    this.AdIDDelivered = adIDDelivered;
    }

        public int SentRecordID;
        public ContentCampaign ContentCampaign;
        public Subscriber Subscriber;
        public DateTime TimeSent;
        public bool Confirmed;
        public string FileSent;
        public int AdIDDelivered;
    }
    

    /\ |_ E X E GG

    F 1 Reply Last reply
    0
    • E eggie5

      How can I get the collection of field values from the below class. So consider this instance of SentRecord SentRecord sr=new SentRecord(1,new Subscriber(), DataTime.Now, false, "C:\img.jpg", 0); Now how can I get all fields and values from sr. e.g.: SentRecordID => 1 ContentCampaign => [ContentCampaign object] Subscriber => [Subscriber object] TimeSent => [DateTime object] Confirmed => false FileSent => "C:\img.jpg" AdIDDelivered => 0

      public class SentRecord : Model
      {
      public SentRecord(int contentID, Subscriber subscriber, DateTime timeSent, bool confirmed, string fileSent, int adIDDelivered)
      {
      this.ContentCampaign.ContentID = contentID;
      this.Subscriber = subscriber;
      this.TimeSent = timeSent;
      this.Confirmed = confirmed;
      this.FileSent = fileSent;
      this.AdIDDelivered = adIDDelivered;
      }

          public int SentRecordID;
          public ContentCampaign ContentCampaign;
          public Subscriber Subscriber;
          public DateTime TimeSent;
          public bool Confirmed;
          public string FileSent;
          public int AdIDDelivered;
      }
      

      /\ |_ E X E GG

      F Offline
      F Offline
      Farhan Noor Qureshi
      wrote on last edited by
      #2

      Look for these classes in MSDN. 1. System.Type 2. System.Reflection.FieldInfo. Call GetField() method of your type and GetValue() method of FieldInfo of your members.


      Farhan Noor Qureshi

      E 1 Reply Last reply
      0
      • F Farhan Noor Qureshi

        Look for these classes in MSDN. 1. System.Type 2. System.Reflection.FieldInfo. Call GetField() method of your type and GetValue() method of FieldInfo of your members.


        Farhan Noor Qureshi

        E Offline
        E Offline
        eggie5
        wrote on last edited by
        #3

        I'm having trouble using GetValue()

        Type t = this.MetaClass.modelObject.GetType();
        this.MetaClass.TableName = t.FullName.Substring(t.FullName.LastIndexOf(".")+1);

                FieldInfo \[\]fields=t.GetFields();
        
                this.MetaClass.fields =new string\[fields.Length\];
                this.MetaClass.values = new string\[fields.Length\];
        
                for (int i = 0; i < fields.Length; i++)
                {
                    this.MetaClass.fields\[i\] = fields\[i\].Name;
                   // object val= fields\[i\].GetValue(fields\[i\]);
                }
        

        I'm able to get all the field names, but I can't get the values of the fields. See the commented line. Any ideas?

        /\ |_ E X E GG

        N 1 Reply Last reply
        0
        • E eggie5

          I'm having trouble using GetValue()

          Type t = this.MetaClass.modelObject.GetType();
          this.MetaClass.TableName = t.FullName.Substring(t.FullName.LastIndexOf(".")+1);

                  FieldInfo \[\]fields=t.GetFields();
          
                  this.MetaClass.fields =new string\[fields.Length\];
                  this.MetaClass.values = new string\[fields.Length\];
          
                  for (int i = 0; i < fields.Length; i++)
                  {
                      this.MetaClass.fields\[i\] = fields\[i\].Name;
                     // object val= fields\[i\].GetValue(fields\[i\]);
                  }
          

          I'm able to get all the field names, but I can't get the values of the fields. See the commented line. Any ideas?

          /\ |_ E X E GG

          N Offline
          N Offline
          Nissim Salomon
          wrote on last edited by
          #4

          Hi Try the following example that iterate over all the fields of an object and print the field name and value

          using System;
          using System.Collections.Generic;
          using System.Text;
          using System.Reflection;

          namespace Test
          {
          public class CustomizeField
          {
          public CustomizeField(int x, int y)
          {
          m_x = x;
          m_y = y;
          }

              public int m\_x;
              public int m\_y;
          
              public override string ToString()
              {
                  return string.Format("X:{0}, Y:{1}", m\_x, m\_y);
              }
          };
          
          public class SentRecord
          {  
              public SentRecord(//int contentID, 
                                //Subscriber subscriber, 
                                DateTime timeSent, 
                                bool confirmed, 
                                string fileSent, 
                                int adIDDelivered) 
              {
                  //this.ContentCampaign.ContentID = contentID; 
                  //this.Subscriber = subscriber; 
                  this.customizeField = new CustomizeField(10, 15);
                  this.TimeSent = timeSent; 
                  this.Confirmed = confirmed; 
                  this.FileSent = fileSent; 
                  this.AdIDDelivered = adIDDelivered; 
              } 
              
              //public int SentRecordID; 
              //public ContentCampaign ContentCampaign; 
              //public Subscriber Subscriber; 
          
              public CustomizeField customizeField;
              public DateTime TimeSent; 
              public bool Confirmed; 
              public string FileSent; 
              public int AdIDDelivered;
          }    
          
          class Program
          {
              static void Main(string\[\] args)
              {
                  SentRecord sr = new SentRecord(DateTime.Now, true, @"c:\\image.jpg", 10);
          
                  FieldInfo\[\] fields = sr.GetType().GetFields();
                  
                  foreach (FieldInfo fi in fields)
                  {
                      Console.WriteLine(fi.Name + " "  + fi.GetValue(sr).ToString());
                  }
              }
          }
          

          }

          E 1 Reply Last reply
          0
          • N Nissim Salomon

            Hi Try the following example that iterate over all the fields of an object and print the field name and value

            using System;
            using System.Collections.Generic;
            using System.Text;
            using System.Reflection;

            namespace Test
            {
            public class CustomizeField
            {
            public CustomizeField(int x, int y)
            {
            m_x = x;
            m_y = y;
            }

                public int m\_x;
                public int m\_y;
            
                public override string ToString()
                {
                    return string.Format("X:{0}, Y:{1}", m\_x, m\_y);
                }
            };
            
            public class SentRecord
            {  
                public SentRecord(//int contentID, 
                                  //Subscriber subscriber, 
                                  DateTime timeSent, 
                                  bool confirmed, 
                                  string fileSent, 
                                  int adIDDelivered) 
                {
                    //this.ContentCampaign.ContentID = contentID; 
                    //this.Subscriber = subscriber; 
                    this.customizeField = new CustomizeField(10, 15);
                    this.TimeSent = timeSent; 
                    this.Confirmed = confirmed; 
                    this.FileSent = fileSent; 
                    this.AdIDDelivered = adIDDelivered; 
                } 
                
                //public int SentRecordID; 
                //public ContentCampaign ContentCampaign; 
                //public Subscriber Subscriber; 
            
                public CustomizeField customizeField;
                public DateTime TimeSent; 
                public bool Confirmed; 
                public string FileSent; 
                public int AdIDDelivered;
            }    
            
            class Program
            {
                static void Main(string\[\] args)
                {
                    SentRecord sr = new SentRecord(DateTime.Now, true, @"c:\\image.jpg", 10);
            
                    FieldInfo\[\] fields = sr.GetType().GetFields();
                    
                    foreach (FieldInfo fi in fields)
                    {
                        Console.WriteLine(fi.Name + " "  + fi.GetValue(sr).ToString());
                    }
                }
            }
            

            }

            E Offline
            E Offline
            eggie5
            wrote on last edited by
            #5

            Hi, how come GetFields doesn't pick up any public properties?

            /\ |_ E X E GG

            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