Reflection Example
-
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
-
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
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
-
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
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
-
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
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()); } } }
}
-
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()); } } }
}