public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues) { FishYuCellStyle fishYuCellStyle = new FishYuCellStyle(); fishYuCellStyle.Alignment = (Alignment)propertyValues["Alignment"]; fishYuCellStyle.BackColor = (Color)propertyValues["BackColor"]; fishYuCellStyle.Font = (Font)propertyValues["Font"]; fishYuCellStyle.ForeColor = (Color)propertyValues["ForeColor"]; fishYuCellStyle.SelectBackColor = (Color)propertyValues["SelectBackColor"]; fishYuCellStyle.SelectForeColor = (Color)propertyValues["SelectForeColor"]; return(fishYuCellStyle);//创建实例 }
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { ////将对象转换为字符串,如:"Jonny,Sun,33" //if ((destinationType == typeof(string)) && (value is FishYuCellStyle)) // return ((FishYuCellStyle)value).Alignment + "," + ((FishYuCellStyle)value).BackColor + "," + ((FishYuCellStyle)value).Font.ToString(); //生成设计时的构造器代码 // this.testComponent1.Person = new CSFramework.MyTypeConverter.Person("Jonny", "Sun", 33); if (destinationType == typeof(InstanceDescriptor) & value is FishYuCellStyle) { FishYuCellStyle p = (FishYuCellStyle)value; ConstructorInfo ctor = typeof(FishYuCellStyle).GetConstructor( new Type[] { typeof(Alignment), typeof(Color), typeof(Font), typeof(Color), typeof(Color), typeof(Color) }); return(new InstanceDescriptor(ctor, new object[] { p.Alignment, p.BackColor, p.Font, p.ForeColor, p.SelectBackColor, p.SelectForeColor })); } return(base.ConvertTo(context, culture, value, destinationType)); }