相关疑难解决方法(0)

如何将枚举与标志组合?

我有一个带标志的枚举.我想声明一个带有n不同标志的变量.n > 1在这种情况下.

public enum BiomeType {
    Warm = 1,
    Hot = 2,
    Cold = 4,
    Intermediate = 8,

    Dry = 16,
    Moist = 32,
    Wet = 64,
}
Run Code Online (Sandbox Code Playgroud)

好的 - 一个变体是将每个标志转换为一个字节并将结果转换为我的枚举.

BiomeType bType = (BiomeType)((byte)BiomeType.Hot + (byte)BiomeType.Dry)
Run Code Online (Sandbox Code Playgroud)

但这有点混乱 - imho.是否有更可读的方式来组合标志?

c# enums

6
推荐指数
1
解决办法
276
查看次数

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

说我有一个enum这样的定义:

[Flags]
public enum LogLevel
{
    None = 1,
    Pages = 2,
    Methods = 4,
    Exception =8
}
Run Code Online (Sandbox Code Playgroud)

和一个类似的类:

public static class Log
{
    public static LogLevel Level = LogLevel.Methods | LogLevel.Pages;

    public static void EnterPage([CallerFilePath]string filePath = "")
    {
        if (Level == //What value here to check if Level includes Pages?)
        {
             //Log
        }
    }
Run Code Online (Sandbox Code Playgroud)

我需要等于什么值Level来检查枚举是否包含Pages

c# enums flags

6
推荐指数
1
解决办法
2273
查看次数

如何比较List <T>中的上一个或下一个元素的属性,而比较字段是动态的?

我上课了

class MarketData
{
    public double Open {get;set;}
    public double High{get;set;}
    public double Low{get;set;}
    public double Close{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

现在我创建了List并填充了最近30天的数据.这是我努力寻找最佳解决方案的地方.用户可以手动输入条件但是采用固定格式,字段可以是例如

Open大于Previous.Close

高于先前.低

低于大于下.高等等

我正在解析字符串条件

public enum ConditionComparer { And, Or }

class SimpleCondition
{
    public string Operand1 { get; set; }
    public Operators Operator { get; set; }
    public string Operand2 { get; set; }
    public ConditionComparer Compare { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在我必须在这个条件上应用List<MarketData>并得到匹配的结果.

我在不同的场景中使用了DynamicQueryable类,其中动态和完美工作的条件,但现在我必须将记录与下一个或上一个记录进行比较.

c# linq dynamic

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

如果按顺序声明[Flags]枚举会怎样?

我遇到过这个问题:[Flags]枚举属性在C#中意味着什么?

有一点我一直想知道,使用接受的答案的例子,如果我宣布会发生什么:

[Flags]
public enum MyColors
{
    Yellow = 1,
    Green = 2,
    Red = 3,
    Blue = 4
}
Run Code Online (Sandbox Code Playgroud)

该示例中的以下步骤是否会导致错误?如果不是,我怎样才能进入MyColors.Red?

c# enum-flags

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

如何创建一个最多接受4个参数的构造函数?

我正在创建一个最多接受4个参数的属性.

我用这种方式编码:

internal class BaseAnnotations
{

    public const string GET = "GET";
    public const string POST = "POST";
    public const string PATCH = "PATCH";
    public const string DELETE = "DELETE";


    public class OnlyAttribute : Attribute
    {
        public bool _GET = false;
        public bool _POST = false;
        public bool _PATCH = false;
        public bool _DELETE = false;

        public OnlyAttribute(string arg1)
        {
            SetMethod(arg1);
        }

        public OnlyAttribute(string arg1, string arg2)
        {
            SetMethod(arg1);
            SetMethod(arg2);
        }

        public OnlyAttribute(string arg1, string arg2, string arg3)
        {
            SetMethod(arg1); …
Run Code Online (Sandbox Code Playgroud)

c#

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

按位将两个if连接成一个

我有这个,

public enum Condition : uint // bitwise
{
    None = 0,
    NewLine = 1,
    Space = 2
}

Rule.Condition someCondition = Rule.Condition.Space | Rule.Condition.NewLine;
Run Code Online (Sandbox Code Playgroud)

我想转换这个,

if ((Rule.Condition.Space & condition) == Rule.Condition.Space) return true;
if ((Rule.Condition.NewLine & condition) == Rule.Condition.NewLine) return true;
Run Code Online (Sandbox Code Playgroud)

进入类似的东西,

if((someCondition & condition) == someCondition) return true;
Run Code Online (Sandbox Code Playgroud)

但它没有用.我忘记了什么?

c# enums bit-manipulation

4
推荐指数
1
解决办法
247
查看次数

If语句的更好的语法

我有这样一个条件:

if (string.IsNullOrEmpty(filename) || size != "Large" || size != "Medium" || size != "Small")
Run Code Online (Sandbox Code Playgroud)

未来的可能性我将不得不sizeif声明中管理更多.

我想知道是否存在一种更易于管理和可读的方式来编写这种情况.

请提供一个真实的例子,感谢您的时间.

.net c# if-statement

4
推荐指数
1
解决办法
254
查看次数

C# - 内部属性在quickwatch中"可读"但不使用反射?

我看到"快速监视"窗口可以访问所有属性,而不管库中类的访问限制(内部,受保护,私有),即使在完全不同的应用程序,库和命名空间中引用库也是如此.虽然我没有找到使用"反射"访问这些的方法.我特别想"阅读"(注意 - 只是阅读)程序集的内部属性.如果通过设计"内部"如何工作(在同一命名空间外无法访问),这是不可能的,那么VS.NET中的"快速监视"窗口是如何"读取"的呢?

这是我使用的示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestLib
{
    public class TestInteralProp
    {
        internal string PropertyInternal
        {
            get { return "Internal Property!";  }
        }

        protected string PropertyProtected
        {
            get { return "Protected Property!"; }
        }

        string PropertyPrivate
        {
            get { return "Private Property!"; }
        }

        public string PropertyPublic
        {
            get { return "Public Property!";  }
        }

        protected internal string PropertyProtectedInternal
        {
            get { return "Protected Internal Property!"; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我为TestInernalProp类创建一个对象时,我可以在quickwatch中看到所有4个属性 -

道具

当我使用反射访问除公共属性(PropertyPublic)之外的任何这些,我得到一个空引用异常. …

c# properties protected access-modifiers internal

4
推荐指数
1
解决办法
2069
查看次数

枚举分配看起来不同

如何分配这个枚举?每个人的价值是多少?

public enum SiteRoles
{
    User = 1 << 0,
    Admin = 1 << 1,
    Helpdesk = 1 << 2
}
Run Code Online (Sandbox Code Playgroud)

分配这样的用途有什么用?

在这篇文章中使用

.net c# c#-4.0

4
推荐指数
1
解决办法
101
查看次数

NHibernate映射按代码级联全删除孤儿

如何在NHibernate中通过代码映射设置级联到all-delete-orphans?

[Flags]
public enum Cascade
{
    None = 0,
    Persist = 2,
    Refresh = 4,
    Merge = 8,
    Remove = 16,
    Detach = 32,
    ReAttach = 64,
    DeleteOrphans = 128,
    All = 256,
}
Run Code Online (Sandbox Code Playgroud)

如何组合All和DeleteOrphans?

nhibernate cascade nhibernate-mapping-by-code

4
推荐指数
1
解决办法
742
查看次数