C#枚举及控件属性问题

qq183670101 2011-05-26 07:11:11

先说目的: 要求控件的属性是下拉列表框(拿字符串做例子)

这样的是可以:
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,
< ,
>=,
<=
}

该怎么办啊,谢谢高手指点。


...全文
382 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
yzx99 2013-10-08
  • 打赏
  • 举报
回复
如果使用另一种方案:控件有两个属性,一个是枚举型的(用于界面设置),一个是文本型的(用于代码处理),界面中,设置枚举型的属性后立即把文本型的相应设置一下。 好处是在使用时,代码量小,代码相对简单,对于控件的使用者(即程序员)更方便,不便考虑转换问题。 这种方案是不是更好?
qq183670101 2011-05-27
  • 打赏
  • 举报
回复
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

}
qq183670101 2011-05-27
  • 打赏
  • 举报
回复
高手都来了,,非常的感谢,我试了上面两种的。也都能实现我的要求

我最后用的是 还是有点不同,因为这样的更方便。


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);


gomoku 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 gomoku 的回复:]
[TypeConverter(typeof(MyEnumConverter))] //<---
enum RelationOp
{
...
[/Quote]
这个MyEnumConverter不能修饰多个枚举。需要更正:
1、可以改为MyEnumConverter<T>
2、或者把字典改为非静态(去掉statis):static Dictionary<Enum, string> DisplayNames...
gomoku 2011-05-27
  • 打赏
  • 举报
回复
public partial class Form1 : Form
{
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);
}
}
}
kingdom_0 2011-05-27
  • 打赏
  • 举报
回复

string[] str = new string[] { "> ", "< ", ">=", "<=" };
机器人 2011-05-27
  • 打赏
  • 举报
回复
class Program
{
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
}
danjiewu 2011-05-27
  • 打赏
  • 举报
回复
foreach (FieldInfo field in typeof(RelationOpert).GetFields(BindingFlags.Public | BindingFlags.Static))
{
object[] atts = field.GetCustomAttributes(typeof(DescriptionAttribute), true);
...
}
qq183670101 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fangxinggood 的回复:]
特殊字符,编译都通不过。当然不可能。

写到枚举的Description里,用反射取出来还能实现。
[/Quote]

感觉这个较好,

private void button1_Click(object sender, EventArgs e)
{
Type ff = typeof(RelationOpert);
.....
//怎么写啊,只能取到名字和值,怎么获取描述呢?
}
}
public enum RelationOpert
{
[Description("=")]
Equal,
[Description(">")]
Great,
Less,
GreatEqual,
LessEqual

}
qq183670101 2011-05-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 caozhy 的回复:]
引用 7 楼 fangxinggood 的回复:
其实你用字符串常量类代替枚举更合适。


C# code
public class RelationOpert
{
public const string Great = ">";
public const string Less = "<";
public const string GreatEqual = ">=";
...……
[/Quote]



是啊,可是在属性中能下拉列表吗, 我也在想到底怎么弄?
qq183670101 2011-05-27
  • 打赏
  • 举报
回复
那就说下完整的需求,这个属性就是为了拼接字符串用的,如果用Great 枚举,在判断的时候也要用if else if
最关键的是: 我要在控件的属性时候,让其他人员选择,只能从4个中选择,并且在用的时候能直接取到> <

等等。
threenewbee 2011-05-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fangxinggood 的回复:]
其实你用字符串常量类代替枚举更合适。


C# code
public class RelationOpert
{
public const string Great = ">";
public const string Less = "<";
public const string GreatEqual = ">=";
...
}
[/Quote]
本来嘛。办法有的是。lz非要为难编译器。
机器人 2011-05-26
  • 打赏
  • 举报
回复
其实你用字符串常量类代替枚举更合适。

public class RelationOpert
{
public const string Great = ">";
public const string Less = "<";
public const string GreatEqual = ">=";
...
}
机器人 2011-05-26
  • 打赏
  • 举报
回复
特殊字符,编译都通不过。当然不可能。

写到枚举的Description里,用反射取出来还能实现。
threenewbee 2011-05-26
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qq183670101 的回复:]
引用 2 楼 caozhy 的回复:
这种设计本来就不合理。


暂不说要求合理不合理, 请问能还是不能。
[/Quote]
你要是能做出来(或者是你要是能找到人能做出来教会你)就是能,否则就是不能。
我觉得你要是能说服微软,为你的需求修改下C#编译器,应该没问题。
qq183670101 2011-05-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 caozhy 的回复:]
这种设计本来就不合理。
[/Quote]

暂不说要求合理不合理, 请问能还是不能。
qq183670101 2011-05-26
  • 打赏
  • 举报
回复
public enum RelationOpert
{
> ,
< ,
>=,
<=
}

什么呀 要求就是让开发者 在使用属性的时候,只能选,而不能随便的输入。
threenewbee 2011-05-26
  • 打赏
  • 举报
回复
这种设计本来就不合理。
tigercao101 2011-05-26
  • 打赏
  • 举报
回复

够特殊,帮你顶!

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧