I have one class that inherits another. I want to use reflection to cycle through the properties and write the values to a file. No problem. Except that I want to control the order in which the properties write out. Is there a clean way to do this? Right now, problem 1 is that the properties in the the subclass write out and THEN the proepries in the parent class write out. But also, I may want to skip some properties or just reorder them.
Here is my code now…
foreach (PropertyInfo bp in t.GetProperties())
{
Type pt = bp.PropertyType;
if ((pt.IsArray) && pt.FullName == "System.Char[]")
{
char[] caPropertyValue;
caPropertyValue = (char[])(bp.GetValue(oBatch, null));
string strPropertyValue = new string(caPropertyValue);
myBatch.Add(strPropertyValue);
}
}
You could create your own Attribute i.e. ”’OrderAttribute”’ and place it over property
such as
in the sorting routine you can check order attribute value via pi.GetCustomAttribute() and sort pi’s over it.