为什么我不能在此函数中使用枚举参数 [英] Why I can't use Enum Parameter in This Function

查看:83
本文介绍了为什么我不能在此函数中使用枚举参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   static   void  LoadHemCombo(ComboBox cbo,Enum myEnum)
{
cbo.DataSource = Enum.GetValues( typeof (myEnum))
.Cast< Enum>()
。选择( = > new
{
(Attribute.GetCustomAttribute( value .GetType()。GetField ( value .ToString()), typeof (DescriptionAttribute)) as DescriptionAttribute)。描述,
value
})
.OrderBy(item = > item。 value
.ToList();
cbo.DisplayMember = 描述;
cbo.ValueMember = value;
}





这个函数在没有enum参数的情况下工作。我发现这个函数在

http://codereview.stackexchange.com/questions/39163/loading-a-combobox-with-an-enum-and-binding-to-it [ ^ ]

解决方案

这:

  typeof (myEnum)

不起作用,因为 myEnum 是你的枚举变量,而不是你的枚举类型。



相反你必须写那里你的枚举定义的名字 - 相当于你链接的来源中的 HemEnum

  public   enum    HemEnum   
{
[描述( none)]
HemNone = -1,
[描述( sewn )]
Hemsew = 0
[描述( 焊接)]
HemWeld = 1
[描述( double fold)]
Hemdoublefold = 2
}



因此,如果您的枚举类型名为Abcdef,您必须写:

  public   static   void  LoadHemCombo(ComboBox cbo)
{
cbo.DataSource = Enum.GetValues( typeof Abcdef ))
// 休息相同



我排除 myEnum 你的方法的参数因为我不知道它应该是什么好处 - 它不需要/在方法中使用。



评论后编辑:



要将其与几种不同的枚举类型一起使用,请将其作为通用方法:



  public   static   void  LoadCombo< TEnum>(ComboBox cbo)
{
cbo.DataSource = Enum.GetValues( typeof (TEnum))
.Cast< Enum>()
.Select( value = > new
{
(Attribute.GetCustomAttribute( value .GetType()。GetField( value .ToString()), typeof (DescriptionAttribute)) as DescriptionAttribute)。描述,
value
} )
.OrderBy(item = > item。 value
.ToList ();
cbo.DisplayMember = 描述;
cbo.ValueMember = value;
}





使用示例:

  public   enum  YourEnumType1 
{
[描述( foo)]
abc,
[描述( bar)]
def,
[描述( baz)]
ghi
}

// < span class =code-comment>您想要初始化应显示YourEnumType1的组合框:

LoadCombo< YourEnumType1>(cbo);


public static void LoadHemCombo(ComboBox cbo,Enum myEnum)
       {
           cbo.DataSource = Enum.GetValues(typeof(myEnum))
               .Cast<Enum>()
               .Select(value => new
               {
                   (Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()), typeof(DescriptionAttribute)) as DescriptionAttribute).Description,
                   value
               })
               .OrderBy(item => item.value)
               .ToList();
           cbo.DisplayMember = "Description";
           cbo.ValueMember = "value";
       }



this function works without enum parameter. I found the function at
http://codereview.stackexchange.com/questions/39163/loading-a-combobox-with-an-enum-and-binding-to-it[^]

解决方案

This:

typeof(myEnum)

doesn't work because myEnum is your enum variable, not your enum type.

Instead you have to write the name of your enum-definition there - the equivalent to what is HemEnum in the source you linked:

public enum HemEnum
{
    [Description("none")]
    HemNone = -1,
    [Description("sewn")]
    Hemsew = 0,
    [Description("welded")]
    HemWeld = 1,
    [Description("double folded")]
    Hemdoublefold = 2
}


So, if your enum type is named "Abcdef" you have to write:

public static void LoadHemCombo(ComboBox cbo)
{
    cbo.DataSource = Enum.GetValues(typeof(Abcdef))
    // rest identical


I excluded the myEnum parameter of your method there because I don't know what it is supposed to be good for - it's not needed/used in the method.

edit after comment:

To use this with several different Enum-Types, make it a generic method:

public static void LoadCombo<TEnum>(ComboBox cbo)
       {
           cbo.DataSource = Enum.GetValues(typeof(TEnum))
               .Cast<Enum>()
               .Select(value => new
               {
(Attribute.GetCustomAttribute(value.GetType().GetField(value.ToString()), typeof(DescriptionAttribute)) as DescriptionAttribute).Description,
                   value
               })
               .OrderBy(item => item.value)
               .ToList();
           cbo.DisplayMember = "Description";
           cbo.ValueMember = "value";
       }



Example use:

public enum YourEnumType1
{
  [Description("foo")]
  abc,
  [Description("bar")]
  def,
  [Description("baz")]
  ghi
}

// wherever you want to initialize the combobox that should show YourEnumType1:

LoadCombo<YourEnumType1>(cbo);


这篇关于为什么我不能在此函数中使用枚举参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆