先说目的: 要求控件的属性是下拉列表框(拿字符串做例子)这样的是可以:
public partial class ViewItem : UserControl
{
private RelationOpert relations; public RelationOpert Relations
{
get { return relations; }
set { relations = value; }
}
} public enum RelationOpert
{
Top = 0,
Left = 1,
Bottom = 2,
Right = 3
}
可是我枚举特殊 public enum RelationOpert
{
> 0,
< ,
>=,
<=
}该怎么办啊,谢谢高手指点。
解决方案 »
- [两段代码干同一件事]到底是什么差别导致了“使用了与请求的协议不兼容的地址”错误???
- 菜鸟求解~~~
- RichTextBox : LinkMouseEnter 和 LinkMouseLeave 处 理 方 法
- WinForm能否和专业监控摄像头结合开发监控软件!
- 如何将窗体form1的变量x的值显示在窗体form2的文本框中?如果x的值变化显示在窗体form2的文本框中的也跟着变化?
- 如何利用自己架设的邮件服务器发信
- 请教线程问题
- 问个鼠标的移动的菜鸟问题!
- VS.NET2000和VS.NET2008的差别
- 怎樣使CrystalReportViewer里可以選擇打印機
- 运用程序域谁能讲解一下
- 有没有人用c#调用过windows 2003的IP安全策略的?
{
> ,
< ,
>=,
<=
}什么呀 要求就是让开发者 在使用属性的时候,只能选,而不能随便的输入。
{
public const string Great = ">";
public const string Less = "<";
public const string GreatEqual = ">=";
...
}
最关键的是: 我要在控件的属性时候,让其他人员选择,只能从4个中选择,并且在用的时候能直接取到> <等等。
感觉这个较好,
{
private void button1_Click(object sender, EventArgs e)
{
Type ff = typeof(RelationOpert);
..... //怎么写啊,只能取到名字和值,怎么获取描述呢?
}
}
public enum RelationOpert
{
[Description("=")]
Equal,
[Description(">")]
Great,
Less,
GreatEqual,
LessEqual }
{
object[] atts = field.GetCustomAttributes(typeof(DescriptionAttribute), true);
...
}
{
static void Main(string[] args)
{
var list = GetEnumDescription(typeof(Test));
} public static List<KeyValuePair<int, string>> GetEnumDescription(Type enumType)
{
var descriptions = new List<KeyValuePair<int, string>>();
FieldInfo[] fis = enumType.GetFields();
foreach (var fi in fis)
{
if (fi.FieldType == typeof(Int32)) continue;
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.
GetCustomAttributes(typeof(DescriptionAttribute), false);
int key = (int)fi.GetValue(null);
string desc = (attributes.Length > 0) ? attributes[0].Description : fi.Name;
descriptions.Add(new KeyValuePair<int, string>(key, desc));
}
return descriptions;
}
}public enum Test
{
[Description(">")]
A,
B,
C
}
string[] str = new string[] { "> ", "< ", ">=", "<=" };
{
public Form1()
{
InitializeComponent();
this.propertyGrid1.SelectedObject = new My(); TypeConverter convert = new MyEnumConverter(typeof(RelationOp));
string g = convert.ConvertToInvariantString(RelationOp.Greater); // >
string e = convert.ConvertToInvariantString(RelationOp.Less); // ==
} class My
{
public string Name { get; set; }
public RelationOp Op { get; set; }
} [TypeConverter(typeof(MyEnumConverter))] //<---
enum RelationOp
{
[Description(">")]
Greater, [Description("<")]
Less, [Description("==")]
Equal,
} class MyEnumConverter : EnumConverter
{
static Dictionary<Enum, string> DisplayNames = new Dictionary<Enum, string>();
public MyEnumConverter(Type t) : base(t)
{
if (DisplayNames.Count == 0)
{
foreach (FieldInfo fi in this.EnumType.GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
{
Enum value = (Enum)fi.GetValue(null);
string display = value.ToString();
foreach(Attribute attr in fi.GetCustomAttributes(false))
{
if(attr is DescriptionAttribute)
{
display = (attr as DescriptionAttribute).Description;
break;
}
}
DisplayNames.Add(value, display);
}
}
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
foreach (Enum key in DisplayNames.Keys)
{
if (string.CompareOrdinal(DisplayNames[key], value as string) == 0) return key;
}
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value is Enum)
{
if (DisplayNames.ContainsKey((Enum)value)) return DisplayNames[(Enum)value];
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
}
1、可以改为MyEnumConverter<T>
2、或者把字典改为非静态(去掉statis):
staticDictionary<Enum, string> DisplayNames...public static string GetEnumDescription(object enumSubitem)
{
enumSubitem=(Enum)enumSubitem;
string strValue = enumSubitem.ToString(); FieldInfo fieldinfo = enumSubitem.GetType().GetField(strValue); if (fieldinfo != null)
{ Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false); if (objs == null || objs.Length == 0)
{
return strValue;
}
else
{
DescriptionAttribute da = (DescriptionAttribute)objs[0];
return da.Description;
}
}
else
{
return "不限";
} }
调用非常简单 string type = GetEnumDescription(controlShow1.RelationOpert);
public partial class ControlShow : UserControl
{
public ControlShow()
{
InitializeComponent();
} private List<int> item = new List<int>(); public List<int> Item
{
get { return item; }
set { item = value; }
} RelationType relationOpert = RelationType.Equal; public RelationType RelationOpert
{
get { return relationOpert; }
set { relationOpert = value; }
} }
public enum RelationType
{
[Description("=")]
Equal,
[Description(">")]
Great,
[Description("<")]
Less,
[Description(">=")]
GreatEqual,
[Description("<=")]
LessEqual }