日期:2014-05-20  浏览次数:20812 次

关于C#开发用户控件->属性的类型转换问题!大家给点提示....
using   System;
using   System.Windows.Forms;
using   System.Drawing;
using   System.Reflection;
using   System.ComponentModel;
using   System.ComponentModel.Design.Serialization;

namespace   UserControl
{
///   <summary>
///   ImageArray   的摘要说明。
///   </summary>
[TypeConverter(typeof(ImageArrayConverter))]
public   class   ImageArray
{
private   Image   _onPress;
private   Image   _onOver;
private   Image   _onOut;
private   Image   _onForbid;

public   ImageArray()
{

}

public   ImageArray(Image   _onPress,   Image   _onOver,   Image   _onOut,   Image   _onForbid)
{
this._onPress   =   _onPress;
this._onOver   =   _onOver;
this._onOut   =   _onOut;
this._onForbid   =   _onForbid;
}

public   Image   OnPress
{
get
{
return   this._onPress;
}
set
{
this._onPress   =   value;
}
}

public   Image   OnOver
{
get
{
return   this._onOver;
}
set
{
this.OnOver   =   value;
}
}

public   Image   OnOut
{
get
{
return   this._onOut;
}
set
{
this._onOut   =   value;
}
}

public   Image   OnForbid
{
get
{
return   this._onForbid;
}
set
{
this._onForbid   =   value;
}
}
}



///   <summary>
///   ImageArrayConverter   的摘要说明。
///   </summary>
public   class   ImageArrayConverter   :   ImageConverter
{
public   override   bool   CanConvertFrom(ITypeDescriptorContext   context,   Type   sourceType)/*1*/
{
if   (sourceType   ==   typeof(String))   return   true;

return   base.CanConvertFrom(context,   sourceType);
}

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

if   (destinationType   ==   typeof(InstanceDescriptor))   return   true;

return   base.CanConvertTo(context,   destinationType);
}

public   override   object   ConvertTo(ITypeDescriptorContext   context,   System.Globalization.CultureInfo   culture,   object   value,   Type   destinationType)/*2*/
{
if   (destinationType   ==   typeof(String))
{
return   "请选择... ";
}

if   (destinationType   ==   typeof(InstanceDescriptor))
{
ConstructorInfo   ci   =   typeof(ImageArray).GetConstructor(new   Type[]   {   typeof(Image),   typeof(Image),   typeof(Image),   typeof(Image)   });
ImageArray   array   =   (ImageArray)value;
return   new   InstanceDescriptor(ci,   new   object[]   {   array.OnPress,   array.OnOver,   array.OnOut,   array.OnForbid   });