using System.Globalization;
using System.Reflection;
public static class typeExtensions
{
//
// savePropertyInfo is a type extension to the Control Class
// it goes through each property and attempts to convert to a string value
// which is added to a List<string>. If the property does not convert to a string
// the cantConvert string literal is added to the List<string>
//
public static void savePropertyInfo(this Control oCont, List<string> psv)
{
//Get Control Type
Type oType = oCont.GetType();
//build property array
PropertyInfo[] propertiesInfoArray = oType.GetProperties();
foreach (PropertyInfo pi in propertiesInfoArray)
{
// get string value for property
psv.Add(GetPropertyValue(pi.PropertyType, oCont, pi.Name));
}
}
//
// cantConvert string literal
//
private static string cantConvert = "Cannot Convert";
//
//Helper method to getPropertyValue as a string
//
private static string GetPropertyValue(Type propertyType, object oObj, string pName)
{
// if property type is string no need for Type Converter
if (propertyType == typeof(string))
{
// Get Control Type
Type type = oObj.GetType();
//use reflection to get value
return type.InvokeMember(
pName,
BindingFlags.GetProperty,
null,
oObj,
null
).ToString();
}
// when type is not string get array of TypeConverterAttribute ..Normally array has one member
object[] attributes = propertyType.GetCustomAttributes(typeof(System.ComponentModel.TypeConverterAttribute), false);
// Go through converters, create instance and check if it can convert to string
foreach (System.ComponentModel.TypeConverterAttribute converterAttribute in attributes)
{
// creater converter instance using Activator
System.ComponentModel.TypeConverter converter = (System.ComponentModel.TypeConverter)Activator.CreateInstance(Type.GetType(converterAttribute.ConverterTypeName));
// check if string is available
if (converter.CanConvertTo(typeof(string)))
{
Type type = oObj.GetType();
// use converter to get property value as string
return converter.ConvertToString(type.InvokeMember(
pName,
BindingFlags.GetProperty,
null,
oObj,
null
));
}
}
// nothing else works
return cantConvert;
}
//
// restorePropertyInfo control Class extension, takes a List<string> created by savePropertyInfo
// and recreates the properties that converted successfully to string values
//
public static void restorePropertyInfo(this Control oCont, List<string> psv)
{
// get type
Type oType = oCont.GetType();
// create properties array
PropertyInfo[] propertiesInfoArray = oType.GetProperties();
//
// Cycle through properties
int index = 0;
foreach (PropertyInfo pi in propertiesInfoArray)
{
// was property value saved OK?
if (psv[index] != cantConvert)
{
// yes
try
{
// set new value
setPropertyValue(pi, pi.PropertyType, oCont, psv[index]);
}
catch { } // ignore problems
}
index;
}
}
//
//Helper method to setPropertyValue from a string
//
private static void setPropertyValue(PropertyInfo pi, Type propertyType, object oObj, string value)
{
//
// first of if property type is string just set value
//
if (propertyType == typeof(string))
{
pi.SetValue(oObj, value, null);
return;
}
//
// Else get converter array and check if converter can do the job
//
object[] attributes = propertyType.GetCustomAttributes(typeof(System.ComponentModel.TypeConverterAttribute), false);
foreach (System.ComponentModel.TypeConverterAttribute converterAttribute in attributes)
{
System.ComponentModel.TypeConverter converter = (System.ComponentModel.TypeConverter)Activator.CreateInstance(Type.GetType(converterAttribute.ConverterTypeName));
if (converter.CanConvertFrom(value.GetType()))
{
// good - use the converter to restore the value
pi.SetValue(oObj, converter.ConvertFromString(value), null);
break;
}
}
}
}