C#自定义属性提示转换失败 visual studio 2008c#编程控件属性 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 之前回答你问题被你忽略了。你根本不懂原理。光重写tostring,那个看着差不多,其实没用。你还是需要实现typeconverter。 你好,我已经实现了typeconverter如下是代码:public class EdgesTypeConverter : TypeConverter { public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); } public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType); } public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { string text = value as string; if (text == null) { return base.ConvertFrom(context, culture, value); } string text2 = text.Trim(); if (text2.Length == 0) { return null; } if (culture == null) { culture = CultureInfo.CurrentCulture; } char c = culture.TextInfo.ListSeparator[0]; string[] array = text2.Split(new char[] { c }); int[] array2 = new int[array.Length]; TypeConverter converter = TypeDescriptor.GetConverter(typeof(int)); for (int i = 0; i < array2.Length; i++) { array2[i] = (int)converter.ConvertFromString(context, culture, array[i]); } if (array2.Length == 4) { return new Edges(array2[0], array2[1], array2[2], array2[3]); } throw new ArgumentException(); } public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { if (destinationType == null) { throw new ArgumentNullException("destinationType"); } if (value is Edges) { if (destinationType == typeof(string)) { Edges edg = (Edges)value; if (culture == null) { culture = CultureInfo.CurrentCulture; } string separator = culture.TextInfo.ListSeparator + " "; TypeConverter converter = TypeDescriptor.GetConverter(typeof(int)); string[] array = new string[4]; int num = 0; array[num++] = converter.ConvertToString(context, culture, edg.Left); array[num++] = converter.ConvertToString(context, culture, edg.Top); array[num++] = converter.ConvertToString(context, culture, edg.Right); array[num++] = converter.ConvertToString(context, culture, edg.Bottom); return string.Join(separator, array); } if (destinationType == typeof(InstanceDescriptor)) { Edges edg2 = (Edges)value; ConstructorInfo constructor = typeof(Edges).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) }); if (constructor != null) { return new InstanceDescriptor(constructor, new object[] { edg2.Left, edg2.Top, edg2.Right, edg2.Bottom }); } } } return base.ConvertTo(context, culture, value, destinationType); } public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues) { if (propertyValues == null) { throw new ArgumentNullException("propertyValues"); } object obj = propertyValues["Left"]; object obj2 = propertyValues["Top"]; object obj3 = propertyValues["Right"]; object obj4 = propertyValues["Bottom"]; if (obj == null || obj2 == null || obj3 == null || obj4 == null || !(obj is int) || !(obj2 is int) || !(obj3 is int) || !(obj4 is int)) { throw new ArgumentException("PropertyValueInvalidEntry"); } return new Edges((int)obj, (int)obj2, (int)obj3, (int)obj4); } public override bool GetCreateInstanceSupported(ITypeDescriptorContext context) { return true; } public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Edges), attributes); return properties.Sort(new string[] { "Left", "Top", "Right", "Bottom" }); } public override bool GetPropertiesSupported(ITypeDescriptorContext context) { return true; } } 请问如何判断一个Object是Action和Func params string[] values如何定义 c#如何改变listview item背景颜色? 多个SQL server2005排序规则。如何才不会乱码 c# 如何调用cmd.exe?? SqlDataReader取值问题~~ 一个比较有意思的多线程的问题 关于单元测试的 弱弱地问一句 c# API语法问题 请问一下VS2003和VS2005怎么开启编译警告啊,装好后默认是没有警告的? 10-100能被3或5整除的数的和。 连接字符串怎么样做更安全
你好,我已经实现了typeconverter
如下是代码:public class EdgesTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
} public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(InstanceDescriptor) || base.CanConvertTo(context, destinationType);
} public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string text = value as string;
if (text == null)
{
return base.ConvertFrom(context, culture, value);
}
string text2 = text.Trim();
if (text2.Length == 0)
{
return null;
}
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
char c = culture.TextInfo.ListSeparator[0];
string[] array = text2.Split(new char[] { c });
int[] array2 = new int[array.Length];
TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
for (int i = 0; i < array2.Length; i++)
{
array2[i] = (int)converter.ConvertFromString(context, culture, array[i]);
}
if (array2.Length == 4)
{
return new Edges(array2[0], array2[1], array2[2], array2[3]);
}
throw new ArgumentException();
} public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if (value is Edges)
{
if (destinationType == typeof(string))
{
Edges edg = (Edges)value;
if (culture == null)
{
culture = CultureInfo.CurrentCulture;
}
string separator = culture.TextInfo.ListSeparator + " ";
TypeConverter converter = TypeDescriptor.GetConverter(typeof(int));
string[] array = new string[4];
int num = 0;
array[num++] = converter.ConvertToString(context, culture, edg.Left);
array[num++] = converter.ConvertToString(context, culture, edg.Top);
array[num++] = converter.ConvertToString(context, culture, edg.Right);
array[num++] = converter.ConvertToString(context, culture, edg.Bottom);
return string.Join(separator, array);
}
if (destinationType == typeof(InstanceDescriptor))
{
Edges edg2 = (Edges)value;
ConstructorInfo constructor = typeof(Edges).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(int) });
if (constructor != null)
{
return new InstanceDescriptor(constructor, new object[] { edg2.Left, edg2.Top, edg2.Right, edg2.Bottom });
}
}
}
return base.ConvertTo(context, culture, value, destinationType);
} public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues == null)
{
throw new ArgumentNullException("propertyValues");
}
object obj = propertyValues["Left"];
object obj2 = propertyValues["Top"];
object obj3 = propertyValues["Right"];
object obj4 = propertyValues["Bottom"];
if (obj == null || obj2 == null || obj3 == null || obj4 == null || !(obj is int) || !(obj2 is int) || !(obj3 is int) || !(obj4 is int))
{
throw new ArgumentException("PropertyValueInvalidEntry");
}
return new Edges((int)obj, (int)obj2, (int)obj3, (int)obj4);
} public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
} public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(Edges), attributes);
return properties.Sort(new string[] { "Left", "Top", "Right", "Bottom" });
} public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
}