我有一个叫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#中发生装箱的所有情况:
将值类型转换为System.Object类型:
struct S { }
object box = new S();
Run Code Online (Sandbox Code Playgroud)将值类型转换为System.ValueType类型:
struct S { }
System.ValueType box = new S();
Run Code Online (Sandbox Code Playgroud)将枚举类型的值转换为System.Enum类型:
enum E { A }
System.Enum box = E.A;
Run Code Online (Sandbox Code Playgroud)将值类型转换为接口引用:
interface I { }
struct S : I { }
I box = new S();
Run Code Online (Sandbox Code Playgroud)在C#字符串连接中使用值类型:
char c = F();
string s1 = "char value will box" + c;
Run Code Online (Sandbox Code Playgroud)
注意:char类型的常量在编译时连接在一起
注意:由于6.0版的C#编译器优化串联涉及bool,char,IntPtr, …
(好吧,我会在这里揭露我无知的深处,请温柔)
背景
我有一个看起来像这样的方法:
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,但编译器不会拥有它.我究竟做错了什么?(请不要说'一切').
我有我的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) 我有一个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) 我有以下声明:
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) 我想比较两个标志,看看它们是否有共同的值。
我希望有一个扩展方法,以便“加速”编码(我将经常在各种枚举类型上使用它)。我怎么能够?
这段代码告诉我:
运算符“&”不能应用于 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# ×7
enums ×6
casting ×2
.net ×1
bindinglist ×1
boxing ×1
enum-flags ×1
generics ×1
int ×1
propertygrid ×1
value-type ×1
wpf ×1