Sprache: C#
The method Inspect returns all properties and their values as a string (purpose: debugging or logging).
Example:
[code]Console.WriteLine(
Inspect(new { Lastname = "Peters", Firstname = "Bob" }));
Console.WriteLine(
Inspect(new DateTime(2015, 11, 20)));[/code]
public static string Inspect(object o)
{
var result = "";
var t = o.GetType();
var infos = t.GetProperties();
foreach (PropertyInfo info in infos)
{
if (info.CanRead)
{
var value = info.GetValue(o);
result += info.Name + " = " + (value == null ? "null" : value.ToString()) + " | ";
}
}
return result;
}
public static string Inspect(object o)
{
var result = "";
var t = o.GetType();
var infos = t.GetProperties();
foreach (PropertyInfo info in infos)
{
if (info.CanRead)
{
var value = info.GetValue(o);
result += info.Name + " = " + (value == null ? "null" : value.ToString()) + " | ";
}
}
return result;
}
Alte URL:
/snippet/inspect-an-object-by-reflection/13087