Automatically presenting information stored in class to user
I am retrieving info from a web service and saving it in a class which has several fields including arrays.
开发者_如何学运维I just want a way to present that info to the user. Is there a component that takes the class as argument and just organizes the info and shows them in a datagridview (for example)?
Thank you.
you use reflection for this issues find out more about this on google.
one of the demo :
static void Main(string[] args)
{
object obj = new Employee();
Type t = obj.GetType();
try
{
System.Reflection.PropertyInfo minfo = t.GetProperties().First(m => m.Name == "ABC");
Console.WriteLine(minfo.Name);
}
catch (Exception ex)
{
Console.WriteLine("Member not found");
}
Console.ReadLine();
}
another example :
protected void setControl(Object obj)
{
Type t = obj.GetType();
foreach (System.Reflection.PropertyInfo minfo in t.GetProperties())
{
try
{
string s = (minfo.GetValue(obj, null)).ToString();
}
catch (Exception ex)
{
Console.WriteLine("Member not found");
}
}
}
Since the response is serializable, you could probably map it pretty easily to something like a TreeView as well. Even though you have a class as your response object, and you can use Reflection to walk over it, I'd probably serialize it and then walk through the XML, though I suppose it's just a point of preference.
If you're just looking for a way to display the properties of your class to the user, you might try the PropertyGrid (http://msdn.microsoft.com/en-us/library/aa302326.aspx).
It allows for a simple representation of common datatypes, but also allows you to add custom representation if you need it.
At its simplest, you just need to set the object you want represented:
propertyGrid1.SelectedObject = myObject;
精彩评论