posts - 50, comments - 140, trackbacks - 8, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理
如果控件的属性是一个数组的时候,如何才能将对象保存下来,又如何将它解析出来呢?使用TypeConverter类可以实现这样的转换。比如今天我所遇到的就是Color[]转换到string保存,并从string解析出来。

控件的CustomColor属性如下定义:
        private Color[] m_CustomColor = new Color[0];

        [Category(
"Appearance"),
        TypeConverter(
typeof(ColorConverter)),
        Description(
"自定义字符随机颜色。")]
        
public Color[] CustomColor
        
{
            
get{return m_CustomColor;}
            
set{m_CustomColor = value;}
        }

然后定义ColorConverter类:
using System;
using System.Drawing;
using System.Globalization;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;

namespace FaibClass.WebControls
{
    
internal class ColorConverter : TypeConverter
    
{
        
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        
{
            
if (sourceType == typeof(string))
            
{
                
return true;
            }


            
return base.CanConvertFrom(context, sourceType);
        }


        
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        
{
            
if (destinationType == typeof(string))
            
{
                
return true;
            }

            
else if (destinationType == typeof(InstanceDescriptor))
            
{
                
return true;
            }


            
return base.CanConvertTo(context, destinationType);
        }


        
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        
{
            
if (value is string)
            
{
                
//从字符串中解析出十六进制格式的颜色值,并转成Color
                string[] value1 = value.ToString().Split(';');
                Color[] col 
= new Color[value1.Length];
                
for(int i = 0; i < value1.Length; i++)
                
{
                    col[i] 
= Util.FromHexColor(value1[i]);
                }

                
return col;
            }


            
return base.ConvertFrom(context, culture, value);
        }


        
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        
{
            
if (destinationType == typeof(string))
            
{
                
//将Color数组转换成 #ffffff;#ccddaa;#00ddcc 这样格式的字符串保存
                Color[] col = (Color[])value;
                
string[] sto = new string[col.Length];
                
for(int i = 0; i < col.Length; i++)
                
{
                    sto[i] 
= Util.ConvertToHexColor(col[i]);
                }

                
return string.Join(";", sto);
            }

            
else if (destinationType == typeof(InstanceDescriptor) && (value is Color[]))
            
{
                Color[] col 
= (Color[])value;
                
string[] sto = new string[col.Length];
                
for(int i = 0; i < col.Length; i++)
                
{
                    sto[i] 
= Util.ConvertToHexColor(col[i]);
                }

                
return new InstanceDescriptor(typeof(Color[]).GetConstructor(new Type[] typeof(string) }), new string[] string.Join(";", sto) } );
            }


            
return base.ConvertTo(context, culture, value, destinationType);
        }

    }

}

以上来用到了两个方法,分别是将颜色转成十六进制格式的值,另一个是反转:
using System.Drawing;
using System.Web.UI.WebControls;

internal class Util
{
        
//将颜色转换为十六进制
        internal static string ConvertToHexColor(Color c)
        
{
            WebColorConverter wcc 
= new WebColorConverter();
            
return wcc.ConvertToString(c);
        }


        
//将颜色转换为十六进制
        internal static Color FromHexColor(string c)
        
{
            WebColorConverter wcc 
= new WebColorConverter();
            
return (Color)wcc.ConvertFromString(c);
        }


}

Feedback

#1楼    回复  引用  查看    

2007-05-18 22:25 by 代码乱了      
这个是可以用在web自定义控件中吗?

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》



相关文章:

相关链接: