相关疑难解决方法(0)

从C#中的枚举中获取int值

我有一个叫Questions(复数)的类.在这个类中有一个名为Question(单数)的枚举,看起来像这样.

public enum Question
{
    Role = 2,
    ProjectFunding = 3,
    TotalEmployee = 4,
    NumberOfServers = 5,
    TopBusinessConcern = 6
}
Run Code Online (Sandbox Code Playgroud)

Questions类中,我有一个get(int foo)函数返回一个Questions对象foo.有没有一种简单的方法从枚举中获取整数值,所以我可以做类似的事情Questions.Get(Question.Role)

c# int enums casting

1698
推荐指数
23
解决办法
142万
查看次数

C#中的拳击事件

我正在尝试收集C#中发生装箱的所有情况:

c# boxing value-type

82
推荐指数
2
解决办法
4007
查看次数

无法将类型'System.Enum'转换为int

(好吧,我会在这里揭露我无知的深处,请温柔)

背景

我有一个看起来像这样的方法:

public void AddLink(Enum enumVal)
{
     string identifier = m_EnumInterpreter(enumVal);
     AddLink(identifier);
}
Run Code Online (Sandbox Code Playgroud)

EnumInterpreter是一个Func <Enum,string>,它在创建父类时传入.

我正在使用Enum,因为在这个级别上它"不关我的事" - 我不关心它是哪个具体的枚举.调用代码只使用(生成的)枚举来避免魔术字符串.

如果EnumInterpreter发回一个空字符串,我想抛出一个带有enumVal实际值的异常.我以为我只能转换为int,但编译器不会拥有它.我究竟做错了什么?(请不要说'一切').

.net c# enums casting

25
推荐指数
2
解决办法
3万
查看次数

获取Enum <T>值描述

我有我的enumHelper类包含这些:

public static IList<T> GetValues()
{
  IList<T> list = new List<T>();
  foreach (object value in Enum.GetValues(typeof(T)))
  {
    list.Add((T)value);
  }
  return list;
}
Run Code Online (Sandbox Code Playgroud)

public static string Description(Enum value)
{
  Attribute DescAttribute = LMIGHelper.GetAttribute(value, typeof(DescriptionAttribute));
  if (DescAttribute == null)
    return value.ToString();
  else
    return ((DescriptionAttribute)DescAttribute).Description;
}
Run Code Online (Sandbox Code Playgroud)

我的枚举是这样的:

public enum OutputType
{
    File,
    [Description("Data Table")]
    DataTable
}
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.所有以前的工作都很好.现在我想添加一个新的帮助器来返回BindingList>,所以我可以将任何枚举链接到任何组合使用

BindingList<KeyValuePair<OutputType, string>> list = Enum<OutputType>.GetBindableList();
cbo.datasource=list;
cbo.DisplayMember="Value";
cbo.ValueMember="Key";
Run Code Online (Sandbox Code Playgroud)

为此,我补充说:

public static BindingList<KeyValuePair<T, string>> GetBindingList()
{
    BindingList<KeyValuePair<T, string>> list = new BindingList<KeyValuePair<T, string>>();
    foreach (T value …
Run Code Online (Sandbox Code Playgroud)

c# generics enums bindinglist

5
推荐指数
2
解决办法
9643
查看次数

显示枚举的int值

我有一个propertygrid,需要在propertygrid内创建一个组合框并显示int值(1到9),我发现使用enum是最简单的方法,但是enum无法显示int值,即使我尝试将其强制转换为int ,但我不知道如何返回所有值。还有其他方法吗?提前致谢。下面是我的代码。

public class StepMode
    {
        private TotalSteps totalSteps;

        public TotalSteps totalsteps
        {
            get { return totalSteps; }
            set { value = totalSteps; }
        }
        public enum TotalSteps
        {
            First = 1,
            Second = 2,
            Three = 3,
            Four = 4,
            Five = 5,
            Six = 6,
            Seven = 7,
            Eight = 8,
            Nine = 9
        }
    }
Run Code Online (Sandbox Code Playgroud)

c# wpf propertygrid enums

5
推荐指数
1
解决办法
191
查看次数

C#显式初始化Enum

我有以下声明:

private Enum _statusValue;
Run Code Online (Sandbox Code Playgroud)

我真的想说的是:

private Enum _statusValue = 0;
Run Code Online (Sandbox Code Playgroud)

即使我知道这是多余的.只是我喜欢明确指定初始化.

但这是不允许的.

是否有一些简单的方法来指定这种声明的初始化?

编辑 - 让我再试一次.

这是我正在做的一个人为/简化的例子.

using System;

namespace EnumTesting
{
   public enum EFixedPhoneUsability
   {
      UnknownValue,       // Should not occur?
      IsUsable,           // User is near the telephone
      NotUsable,          // User not near the telephone
      BeingUsed,          // Busy
      DoNotDisturb,       // This is phone physical state, not user attitude
      CallsForwarded      // This is phone physical state
   }

   public enum EMobilePhoneConnectivity
   {
      UnknownValue,       // Should not occur?
      OnNetIdle,          // I.e., usable
      OnNetBusy, …
Run Code Online (Sandbox Code Playgroud)

c# enums initialization

1
推荐指数
1
解决办法
2万
查看次数

检查标志是否包含其他标志的任何值

我想比较两个标志,看看它们是否有共同的值。
我希望有一个扩展方法,以便“加速”编码(我将经常在各种枚举类型上使用它)。我怎么能够?
这段代码告诉我:

运算符“&”不能应用于 enum 和 enum 类型的操作数

public enum Tag
{
    Value1 = 1,
    Value2 = 2,
    Value3 = 4,
    Value4 = 8,
    Value5 = 16
}

public static class FlagExt
{
    public static bool HasAny(this Enum me, Enum other)
    {
        return (me & other) != 0;
    }
}

public class Program
{
    public static void Main()
    {
        Tag flag1 = Tag.Value1 | Tag.Value2 | Tag.Value3;
        Tag flag2 = Tag.Value2 | Tag.Value3 | Tag.Value4;
        
        Console.WriteLine(flag1.HasAny(flag2)); // it should returns …
Run Code Online (Sandbox Code Playgroud)

c# enums enum-flags

0
推荐指数
1
解决办法
761
查看次数