Is there a way in c# to loop over the properties of a class?
Basically I have a class that contains a large number of property\'s (it basically holds the results of
I tried various recommendations on this page, I couldn't quite get any of them to work. I started with the top answer (you'll notice my variables are similarly named), and just worked through it on my own. This is what I got to work - hopefully it can help someone else.
var prop = emp.GetType().GetProperties(); //emp is my class
foreach (var props in prop)
{
var variable = props.GetMethod;
empHolder.Add(variable.Invoke(emp, null).ToString()); //empHolder = ArrayList
}
***I should mention this will only work if you're using get;set; (public) properties.
string target = "RECEIPT_FOOTER_MESSAGE_LINE" + index + "Column";
PropertyInfo prop = xEdipV2Dataset.ReceiptDataInfo.GetType().GetProperty(target);
Type t = xEdipV2Dataset.ReceiptDataInfo.RECEIPT_FOOTER_MESSAGE_LINE10Column.GetType();
prop.SetValue(t, fline, null);
Attent for that: target object must be setable
Looping over properties
Type t = typeof(MyClass);
foreach (var property in t.GetProperties())
{
}
Sure; you can do that in many ways; starting with reflection (note, this is slowish - OK for moderate amounts of data though):
var props = objectType.GetProperties();
foreach(object obj in data) {
foreach(var prop in props) {
object value = prop.GetValue(obj, null); // against prop.Name
}
}
However; for larger volumes of data it would be worth making this more efficient; for example here I use the Expression
API to pre-compile a delegate that looks writes each property - the advantage here is that no reflection is done on a per-row basis (this should be significantly faster for large volumes of data):
static void Main()
{
var data = new[] {
new { Foo = 123, Bar = "abc" },
new { Foo = 456, Bar = "def" },
new { Foo = 789, Bar = "ghi" },
};
string s = Write(data);
}
static Expression StringBuilderAppend(Expression instance, Expression arg)
{
var method = typeof(StringBuilder).GetMethod("Append", new Type[] { arg.Type });
return Expression.Call(instance, method, arg);
}
static string Write<T>(IEnumerable<T> data)
{
var props = typeof(T).GetProperties();
var sb = Expression.Parameter(typeof(StringBuilder));
var obj = Expression.Parameter(typeof(T));
Expression body = sb;
foreach(var prop in props) {
body = StringBuilderAppend(body, Expression.Property(obj, prop));
body = StringBuilderAppend(body, Expression.Constant("="));
body = StringBuilderAppend(body, Expression.Constant(prop.Name));
body = StringBuilderAppend(body, Expression.Constant("; "));
}
body = Expression.Call(body, "AppendLine", Type.EmptyTypes);
var lambda = Expression.Lambda<Func<StringBuilder, T, StringBuilder>>(body, sb, obj);
var func = lambda.Compile();
var result = new StringBuilder();
foreach (T row in data)
{
func(result, row);
}
return result.ToString();
}
You can make a list of oject's properties
IList<PropertyInfo> properties = typeof(T).GetProperties().ToList();
And then use a foreach to navigate into the list..
foreach (var property in properties)
{
here's code...
}
Use something like
StringBuilder csv = String.Empty;
PropertyInfo[] ps this.GetType().GetProperties();
foreach (PropertyInfo p in ps)
{
csv.Append(p.GetValue(this, null);
csv.Append(",");
}