How do I retrieve a value from a [Flag] enum property?

This question already has an answer here: What does the [Flags] Enum Attribute mean in C#? 10 answers First of all, flags must have a None = 0 value, because otherwise there's no way to represent a 0 mask (ie no value). Once you've fixed it, you can check if some enumeration value is in some given flag using Enum.HasFlag or the bit-wise & operator: Level.HasFlag(LogLevel.Pages

如何从[Flag]枚举属性中检索值?

这个问题在这里已经有了答案: 在C#中,[Flags]枚举属性意味着什么? 10个答案 首先,标志必须有一个None = 0值,否则无法表示0掩码(即没有值)。 一旦你固定它,你可以检查一些枚举值是在使用一些定标志Enum.HasFlag或逐位&操作: Level.HasFlag(LogLevel.Pages); ...要么: (Level & LogLevel.Pages) == LogLevel.Pages 最后,当你实现一个标志枚举时,通常枚举标识符用复数表示。 在你的情况,你应该重

Logical operators on enums

This question already has an answer here: What does the [Flags] Enum Attribute mean in C#? 10 answers If you're going to use the enum as a bitmap like this, then the members need to be given values which use a different bit each: [Flags] enum MyEnum { Bold = 0x01, Italic = 0x02, Underlined = 0x04, Struck = 0x08 } By default, they've been given the numbers 0,1,2,3 - the

逻辑运算符枚举

这个问题在这里已经有了答案: 在C#中,[Flags]枚举属性意味着什么? 10个答案 如果你打算使用enum作为这样的位图,那么需要给成员赋予不同的值: [Flags] enum MyEnum { Bold = 0x01, Italic = 0x02, Underlined = 0x04, Struck = 0x08 } 默认情况下,他们被赋予了数字0,1,2,3 - 第一个不做任何事情,第二个与最后一个重叠。 正如在注释中提到的,您还应该将[Flags]属性添加到枚举定义中,这样如果您执

Enum flag attribute C#

This question already has an answer here: What does the [Flags] Enum Attribute mean in C#? 10 answers First of, you need to manually number your values with the powers-of-2 sequence : [Flags] private enum MyEnum { Apple = 1, Orange = 2, Tomato = 4, Potato = 8, Melon = 16, Watermelon = 32, Fruit = Apple | Orange, Vegetable = Tomato | Potato, Berry = Melon | Watermelon, }

枚举标志属性C#

这个问题在这里已经有了答案: 在C#中,[Flags]枚举属性意味着什么? 10个答案 首先,您需要使用powers-of-2序列手动为您的值编号: [Flags] private enum MyEnum { Apple = 1, Orange = 2, Tomato = 4, Potato = 8, Melon = 16, Watermelon = 32, Fruit = Apple | Orange, Vegetable = Tomato | Potato, Berry = Melon | Watermelon, } [Flags]属性不是严格必要的,它只控制ToString()行为。 为

Can not cast integer to Enum in Generic method

This question already has an answer here: Cast Int to Generic Enum in C# 4 answers As a workaround, instead of (T)x.ToInteger() you should be able to do: (T)(object)x.ToInteger() . This involves boxing and unboxing the value but that most likely is not a significant performance issue here.

无法将整数转换为通用方法中的枚举

这个问题在这里已经有了答案: 在C#4中将Int转换为通用枚举答案 作为一种解决方法,而不是(T)x.ToInteger()你应该能够做到: (T)(object)x.ToInteger() 。 这涉及到装箱和拆箱的价值,但这很可能不是一个重要的性能问题。

Generic enum constraint

I have inherited a web api that has lots of enums defined in code, I want to convert them to a view-model class called EnumView so they can be serialized as below... {Id: value, Name: enumName} public class EnumView { public int Id { get; set; } public string Name { get; set; } } Upon restricting the Generic class to the enum type, I get the warning Constraint cannot be special clas

泛型枚举约束

我已经继承了一个web API,它有很多代码中定义的枚举,我想将它们转换为名为EnumView的视图模型类,以便它们可以如下所示序列化... {Id:value,Name:enumName} public class EnumView { public int Id { get; set; } public string Name { get; set; } } 将泛型类限制为枚举类型后,我得到警告 约束不能是特殊的类'System.Enum' 这是我正在使用的通用转换器... public class EnumViewConverter<T&g

C# How to check an enum value is passed in a parameter?

I have a method which accepts an enum as an argument: [Flags] public enum MyEnum { A = 1, B = 2, C = 3 } public class MyClass { public MyEnum myEnum; } public bool MyMethod(MyClass class, MyEnum enumParam) { // Here I want to check if object class contains some enum values passed as an argument // Something like: if(class.myEnum 'contains-one-of-the-items-of enumParam) } public v

C#如何检查枚举值传递给参数?

我有一个方法接受一个枚举作为参数: [Flags] public enum MyEnum { A = 1, B = 2, C = 3 } public class MyClass { public MyEnum myEnum; } public bool MyMethod(MyClass class, MyEnum enumParam) { // Here I want to check if object class contains some enum values passed as an argument // Something like: if(class.myEnum 'contains-one-of-the-items-of enumParam) } public void Test() { Mycl

Why use flags+bitmasks rather than a series of booleans?

Given a case where I have an object that may be in one or more true/false states, I've always been a little fuzzy on why programmers frequently use flags+bitmasks instead of just using several boolean values. It's all over the .NET framework. Not sure if this is the best example, but the .NET framework has the following: public enum AnchorStyles { None = 0, Top = 1, Bottom

为什么使用标志+位掩码而不是一系列布尔值?

鉴于我有一个对象可能处于一个或多个真/假状态的情况,我总是对为什么程序员经常使用flags +位掩码而不是使用多个布尔值有点模糊。 它遍布.NET框架。 不知道这是否是最好的例子,但.NET框架具有以下内容: public enum AnchorStyles { None = 0, Top = 1, Bottom = 2, Left = 4, Right = 8 } 所以给定一个锚样式,我们可以使用位掩码来找出哪些状态被选中。 但是,您似乎可以使用为每个可能的值定义的

Enum type constraints in C#

Possible Duplicate: Anyone know a good workaround for the lack of an enum generic constraint? What is the reason behind C# not allowing type constraints on Enum 's? I'm sure there is a method behind the madness, but I'd like to understand why it's not possible. Below is what I would like to be able to do (in theory). public static T GetEnum<T>(this string description

C#中的枚举类型约束

可能重复: 任何人都知道缺乏枚举通用约束的一个很好的解决方法? C#背后的原因是不允许Enum的类型限制? 我确信疯狂背后有一种方法,但我想明白为什么这是不可能的。 以下是我希望能够做到的(理论上)。 public static T GetEnum<T>(this string description) where T : Enum { ... } 这是偶尔要求的功能。 正如我所指出的那样,除非有人设计,规格说明,实现,测试,文档和发布该功能,否则所有功能都未实

C# naming convention for enum and matching property

I often find myself implementing a class maintaining some kind of own status property as an enum: I have a Status enum and ONE Status property of Status type. How should I solve this name conflict? public class Car { public enum Status { Off, Starting, Moving }; Status status = Status.Off; public Status Status // <===== Won't compile ===== { get { return status;

枚举和匹配属性的C#命名约定

我经常发现自己实现了一个类来维护某种自己的状态属性作为一个枚举:我有一个状态枚举和状态类型的一个状态属性。 我应该如何解决这个名称冲突? public class Car { public enum Status { Off, Starting, Moving }; Status status = Status.Off; public Status Status // <===== Won't compile ===== { get { return status; } set { status = value; DoSomething(); } } } 如果状态

Algorithm to detect overlapping periods

I've to detect if two time periods are overlapping. Every period has a start date and an end date. I need to detect if my first time period (A) is overlapping with another one(B/C). In my case, if the start of B is equal to the end of A, they are not overlapping(the inverse too) I found the following cases: So actually I'm doing this like this: tStartA < tStartB && t

检测重叠周期的算法

我必须检测两个时间段是否重叠。 每个时期都有开始日期和结束日期。 我需要检测我的第一个时间段(A)是否与另一个(B / C)重叠。 在我的情况下,如果B的开始等于A的结束,它们不重叠(也是相反的) 我发现了以下情况: 所以其实我是这样做的: tStartA < tStartB && tStartB < tEndA //For case 1 OR tStartA < tEndB && tEndB <= tEndA //For case 2 OR tStartB < tStartA &&a