In most projects there is always a requirement for tracing and/or writing debugging information along with writing out log information. When dealing with objects it is usually up to the developer to override the ToString() method to ensure meaningful information is presented. However this does not always happen. Reflection presents an alternative solution to this problem, and one that i recently adopted on a project.
Rendering an Object using ReflectionThe power of reflection, in this scenario, is that it allows one to easily navigate through an object’s properties and determine the corresponding name value pair. This then allows one to construct a string that can be easily rendered. Rather than run through small code snippets I have included is a helper class that will support passing in either an object, or an object collection, and creates a string representation of the object. The complete listing can be found at the bottom of this blog. The key to making this work is the ability of reflection to iterate through an objects properties and determine the associated value.Type objectType = item.GetType();
foreach (PropertyInfo propertyInfo in objectType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (propertyInfo.CanRead)
{
string value = ProcessProperty(propertyInfo.PropertyType, propertyInfo.Name, propertyInfo.GetValue(item, null)); // Code goes here
}
}
Once the value has been determined, the type is inspected, and converted to a string. You should also notice that if an object returns a meaningful value from the ToString() call this this value is returned.string valueString = item.ToString();
Type objectType = item.GetType();
if (!objectType.IsValueType && valueString == objectType.FullName)
{
valueString = GetObjectValues(item, multipleLine);
}
In terms of using the code it is as easy as calling the ParameterCollectionToString or ParameterObjectToString method, within the debugging, logging, or tracing call.
Dealing with Sensitive InformationOne issue that will arise in adopting such a generic solution, rather than one specific to each object (the ToString() solution) is how we hide Sensitive or Personally Identifiable Information. Take banking or medical applications for instance. Any tracing or logging solution should never disclose information such as bank account numbers or sensitive medical information. This is where attributes can help.
Read more: Carl's Blog
QR:
Rendering an Object using ReflectionThe power of reflection, in this scenario, is that it allows one to easily navigate through an object’s properties and determine the corresponding name value pair. This then allows one to construct a string that can be easily rendered. Rather than run through small code snippets I have included is a helper class that will support passing in either an object, or an object collection, and creates a string representation of the object. The complete listing can be found at the bottom of this blog. The key to making this work is the ability of reflection to iterate through an objects properties and determine the associated value.Type objectType = item.GetType();
foreach (PropertyInfo propertyInfo in objectType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
if (propertyInfo.CanRead)
{
string value = ProcessProperty(propertyInfo.PropertyType, propertyInfo.Name, propertyInfo.GetValue(item, null)); // Code goes here
}
}
Once the value has been determined, the type is inspected, and converted to a string. You should also notice that if an object returns a meaningful value from the ToString() call this this value is returned.string valueString = item.ToString();
Type objectType = item.GetType();
if (!objectType.IsValueType && valueString == objectType.FullName)
{
valueString = GetObjectValues(item, multipleLine);
}
In terms of using the code it is as easy as calling the ParameterCollectionToString or ParameterObjectToString method, within the debugging, logging, or tracing call.
Dealing with Sensitive InformationOne issue that will arise in adopting such a generic solution, rather than one specific to each object (the ToString() solution) is how we hide Sensitive or Personally Identifiable Information. Take banking or medical applications for instance. Any tracing or logging solution should never disclose information such as bank account numbers or sensitive medical information. This is where attributes can help.
Read more: Carl's Blog
QR:
0 comments:
Post a Comment