Search KB Filter article types
Saving Control Properties
Categories: Skinnable and Inherited Controls
Tags: Developers, 2. Intermediate, 3. Advanced, Customization, Pre v6.3, v6.3, v6.4 and Later
Revision: 1
Posted: 10/Jan/2008
Updated: 10/Jan/2008
Status: Publish
Types: Article

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;
                }
            }           
        }
 
}

****************************************************************************************************************
** Example Use **
****************************************************************************************************************

Label l = new Label();
 
Panel p = new Panel();
p.Controls.Add(l);
properyGrid1.SelectedObject = l;
...
...
...
List<string> savePropertiesList = new List<string>();
l.savePropertyInfo(savePropertiesList);
//
//Persist savePropertiesList to suitable storage if required
//
// meanwhile on another planet
 
List<string> savePropertiesList = getPropertiesListFromStorage();
 
Label l = new Label();
 
Panel p = new Panel();
l.restorePropertyInfo(savePropertiesList);
p.Controls.Add(l);

About the author

Related Articles

Skinnable and Inherited Controls  
Title Update Author
Hi everyone, I develop a simple inputbox class with some features like value checking, maximum value size, etc. It is used as the MessageBox class and you can modify it at your needs. Check the attached sample application. Regards, Scott.
Tags: Developers, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
02/Jan/2010    2010/01/02
Tags: Developers, Data Binding, 2. Intermediate, 3. Advanced, Customization, Layouting, Pre v6.3, v6.3, v6.4 and Later
05/Jan/2009    2009/01/05
Tags: Developers, Gateway, 2. Intermediate, 3. Advanced, Customization, Integration, Pre v6.3, v6.3, v6.4 and Later
09/Jan/2008    2008/01/09
Control of a combobox with a checked list and a tree.
Tags: Developers, Navigation, 2. Intermediate, 3. Advanced, Customization, Layouting, Navigation, Pre v6.3, v6.3, v6.4 and Later
06/Jan/2008    2008/01/06
Tags: Developers, Data Binding, Windows & Dialogs, 1. Beginner, 2. Intermediate, Customization, Data Binding, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
03/Jan/2009    2009/01/03
Code snippet for custom Drag&Drop image
Tags: Developers, Drag & Drop, Resource Handlers, C#, XSLT, 2. Intermediate, 3. Advanced, Customization, DHTML, v6.4 and Later
08/June/2011    2011/06/08
.NET Web, Cloud and Mobile application delivery platform | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2011 Visual WebGui®       Visual WebGui weblog on ASP.NET Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS