概述
BrowsableAttribute, // false可见性
CategoryAttribute, //分类
DescriptionAttribute //说明注释
DisplayNameAttribute //属性别名
ReadOnlyAttribute (true)//只读
PropertyOrder(10)//排序—要自定义
[Browsable(true), Category(“布局”),Description(“just a test”)]
别称:[DisplayName(“名称”)]
可编辑性:[EditorBrowsable(EditorBrowsableState.Always)]
提供在可扩展对象与其他各种表示形式之间实现转换的类型转换器:
[TypeConverter(typeof(ExpandableObjectConverter))]//多层属性
放在类前面
[TypeConverter(typeof(ExpandSortConverter))]
[DisplayName(“最小值”), PropertyOrder(1)]
放在属性前面
public class ExpandSortConverter : ExpandableObjectConverter
{
#region Methods
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
//
// This override returns a list of properties in order
//
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes);
ArrayList orderedProperties = new ArrayList();
foreach (PropertyDescriptor pd in pdc)
{
Attribute attribute = pd.Attributes[typeof(PropertyOrderAttribute)];
if (attribute != null)
{
//
// If the attribute is found, then create an pair object to hold it
//
PropertyOrderAttribute poa = (PropertyOrderAttribute)attribute;
orderedProperties.Add(new PropertyOrderPair(pd.Name, poa.Order));
}
else
{
//
// If no order attribute is specifed then given it an order of 0
//
orderedProperties.Add(new PropertyOrderPair(pd.Name, 0));
}
}
//
// Perform the actual order using the value PropertyOrderPair classes
// implementation of IComparable to sort
//
orderedProperties.Sort();
//
// Build a string list of the ordered names
//
ArrayList propertyNames = new ArrayList();
foreach (PropertyOrderPair pop in orderedProperties)
{
propertyNames.Add(pop.Name);
}
//
// Pass in the ordered list for the PropertyDescriptorCollection to sort by
//
return pdc.Sort((string[])propertyNames.ToArray(typeof(string)));
}
#endregion
#region 类型转换
//该方法判断此类型可以转换为哪些类型
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
//调用基类方法处理其他情况
return base.CanConvertTo(context, destinationType);
}
//该方法判断哪些类型可以转换为此类型
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
// 将该类型转换为字符串
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value != null)
{
SecondProperty t = (SecondProperty)value;
string str = t.Min + "," + t.Max;
return str;
}
return base.ConvertTo(context, culture, value, destinationType);
}
//字符串转换为该类
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
{
string str = (string)value;
str = str.Trim();
string[] v = str.Split(',');
if (v.Length != 2)
{
throw new NotSupportedException("Invalid parameter format");
}
int max = 0;
int min = 0;
bool res = int.TryParse(v[0], out min);
if (res == false) throw new NotSupportedException("Invalid parameter format");
res = int.TryParse(v[1], out max);
if (res == false) throw new NotSupportedException("Invalid parameter format");
SecondProperty t = new SecondProperty(min, max);
return t;
}
return base.ConvertFrom(context, culture, value);
}
#endregion
}
#region Helper Class - PropertyOrderAttribute
[AttributeUsage(AttributeTargets.Property)]
public class PropertyOrderAttribute : Attribute
{
//
// Simple attribute to allow the order of a property to be specified
//
private int _order;
public PropertyOrderAttribute(int order)
{
_order = order;
}
public int Order
{
get
{
return _order;
}
}
}
#endregion
#region Helper Class - PropertyOrderPair
public class PropertyOrderPair : IComparable
{
private int _order;
private string _name;
public string Name
{
get
{
return _name;
}
}
public PropertyOrderPair(string name, int order)
{
_order = order;
_name = name;
}
public int CompareTo(object obj)
{
//
// Sort the pair objects by ordering by order value
// Equal values get the same rank
//
int otherOrder = ((PropertyOrderPair)obj)._order;
if (otherOrder == _order)
{
//
// If order not specified, sort by name
//
string otherName = ((PropertyOrderPair)obj)._name;
return string.Compare(_name, otherName);
}
else if (otherOrder > _order)
{
return -1;
}
return 1;
}
}
#endregion
借鉴
属性自定义排序方法
自定义转换类型
最后
以上就是忧伤冬天为你收集整理的C#中ExpandSortConverter扩展排序转换器的全部内容,希望文章能够帮你解决C#中ExpandSortConverter扩展排序转换器所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复