beginning C# for real. I'm trying to implement TypeDescriptor.GetProperties. In the code I keep on getting an empty collection. I can't figure out why.
Any help is appreciated. Thanks.
public class SampleObjectToExportI
{
public Guid Id;
public DateTime Date;
public string StringValue;
public int NumberValue;
public bool BooleanValue;
public SampleObjectToExportI(Guid id, DateTime date, string stringValue, int numberValue, bool booleanValue)
{
this.Id = id;
this.Date = date;
this.StringValue = stringValue;
this.NumberValue = numberValue;
this.BooleanValue = booleanValue;
}
}
class Program
{
static void Main(string[] args)
{
var myList = new List<SampleObjectToExportI>();
myList.Add(new SampleObjectToExportI(Guid.NewGuid(), DateTime.Now, "String4", 400, true));
myList.Add(new SampleObjectToExportI(Guid.NewGuid(), DateTime.Now, "String5", 500, false));
myList.Add(new SampleObjectToExportI(Guid.NewGuid(), DateTime.Now, "String6", 600, true));
foreach (var sampleObjectToExport in myList)
{
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(sampleObjectToExport))
{
string name = descriptor.Name;
object value = descriptor.GetValue(sampleObjectToExport);
Console.WriteLine("{0}={1}", name, value);
}
//Console.WriteLine(sampleObjectToExport.NumberValue);
}
}
}
You don't have properties, if you want to then this is the syntax:
public Guid Id { set; get; }
public DateTime Date { set; get; }
public string StringValue { set; get; }
public int NumberValue { set; get; }
public bool BooleanValue { set; get; }
Refer to documentation for more info.
Another way is to keep your code as is and use Type.GetFeilds()
:
foreach (var fieldInfo in typeof(SampleObjectToExportI).GetFields())