相关疑难解决方法(0)

旗帜枚举混乱C#

根据我的代码a = 1,b = 2,c = 3等我认为该标志会使a = 1,b = 2,c = 4等

[Flags]
public enum someEnum { none, a, b, c, d, e, f, }
Run Code Online (Sandbox Code Playgroud)

我如何得到我的意图(c = 4,e = 8)?[Flags]上面的意思是什么?

c# enums attributes enum-flags

3
推荐指数
1
解决办法
1188
查看次数

更改WinForms ComboBox的选择颜色

所有,我有一个深刻的外观,但似乎无法找到我要找的东西.我想要改变ComboBoc控件的选择颜色(理想情况下不必对控件进行子类化).我虽然做了以下工作,但这个事件甚至没有解雇

private void comboBoxDb_DrawItem(object sender, DrawItemEventArgs e) 
{
    ComboBox combo = sender as ComboBox;
    e.Graphics.FillRectangle(new SolidBrush(combo.BackColor), e.Bounds);
    string strSelectionColor = @"#99D4FC";
    Color selectionColor = 
        System.Drawing.ColorTranslator.FromHtml(strSelectionColor);
    e.Graphics.DrawString(combo.Items[e.Index].ToString(), 
                          e.Font, 
                          new SolidBrush(selectionColor), 
                          new Point(e.Bounds.X, e.Bounds.Y));
}
Run Code Online (Sandbox Code Playgroud)

但这个事件甚至没有开火.我在这做错了什么?

谢谢你的时间.

编辑.尽管非触发是由于未设置@Teppic正确指出的ComboBox的DrawMode属性引起的,但这仍然没有按照我的要求进行.我想设置选择颜色,我上面做了什么(我在这里阻止了名字)

NotWhatIsRequired

而我想更改控件的蓝色突出显示,如此处所示.

在此输入图像描述

c# combobox winforms

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

HasFlag始终返回True

有办法检查我是否在一系列旗帜中有旗帜?

例:

[Flags]
Enum TestEnum
{
  ALIVE, DEAD, ALMOSTDEAD, HURT, OTHERS

}
// check if is alive and has been hurt
TestEnum aTest = TestEnum.ALIVE | TestEnum.HURT
bool aTest2 = aTest.HasFlag(TestEnum.ALIVE)
Run Code Online (Sandbox Code Playgroud)

a.Test.HasFlag即使没有TestEnum.ALIVE,总是返回true

c# enums

3
推荐指数
1
解决办法
1257
查看次数

匹配数组中的整数

我有以下整数,我想用if语句检查值.

int myInt = 3;
Run Code Online (Sandbox Code Playgroud)

我尝试了这个有效的代码,但我不喜欢一遍又一遍地编写变量名.

if (myInt == 0 || myInt == 2 || myInt == 3 || myInt == 4)
{
    Debug.WriteLine("Match");              
}
else
{
    Debug.WriteLine("No Match");
}
Run Code Online (Sandbox Code Playgroud)

为了展示我想要的理想,我尝试过这样的事情:

if (myInt == (0 | 2 | 3 | 4))
{
    Debug.WriteLine("Match");
}
else
{
    Debug.WriteLine("No Match");
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用,因为| 由于存在类型不匹配,因此不是正确的运算符.

然后我尝试了这个,它也工作正常,但我仍然不喜欢声明一个额外的数组.

if ((new int[] { 0, 2, 3, 4 }).Contains(myInt))
{
    Debug.WriteLine("Match");
}
else
{
    Debug.WriteLine("No Match");
}
Run Code Online (Sandbox Code Playgroud)

问题是:

是否有一个运算符可以满足我想要完成的任务而无需声明一个额外的数组或一遍又一遍地询问相同的变量名称|| 运营商?

c# arrays int if-statement

3
推荐指数
1
解决办法
126
查看次数

如何检查至少一个枚举值是否等于if条件中的变量

检查if条件中多个变量的相等性的常用方法如下.

public enum Values
{
    Value1,
    Value2,
    Value3
}
void MethodName (Values randomValue )
{
 if (randomValue == Values.Value1|| randomValue == Values.Value2)
  {
       // code here
  }
}
Run Code Online (Sandbox Code Playgroud)

而不是有一个OR条件,有没有更好的方法来做到这一点?

c# enums

3
推荐指数
1
解决办法
2914
查看次数

标志和<<枚举上的操作?C#

好的,所以我是C#的新手,对于我的生活,我无法理解下面的代码(来自遗留项目)应该做什么:

[Flags]
public enum EAccountStatus
{
    None = 0,
    FreeServiceApproved = 1 << 0,
    GovernmentAccount = 1 << 1,
    PrivateOrganisationAccount = 1 << 2,
    All = 8
}
Run Code Online (Sandbox Code Playgroud)

<<操作员在枚举中到底做了什么?我们为什么需要这个?

c# enums bit-manipulation

3
推荐指数
1
解决办法
1863
查看次数

在[授权(角色)]上使用枚举[标志]和按位运算符

我建立一个ASP.NET MVC应用5,并按照这个例子SO发布适用[Flags]enum:

[Flags]
public enum Role
{
    User = 1,
    Finance = 2,
    Management = 4,
    SystemAdmin = 8
}
Run Code Online (Sandbox Code Playgroud)

请注意,该[Flags]属性为我们提供了一种很好的方法来将多个值作为一个逗号分隔的字符串:

var roles = (Constants.Role.SystemAdmin | Constants.Role.Management).ToString();
// "SystemAdmin, Management"
Run Code Online (Sandbox Code Playgroud)

枚举值将[Authorize]在控制器和操作的属性中使用,例如:

[Authorize(Roles = (Constants.Role.SystemAdmin | Constants.Role.Management).ToString())] 
public class SomeController : Controller
{ ... }
Run Code Online (Sandbox Code Playgroud)

但是,以上属性会生成此错误:

属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式

为什么我可以使用该.ToString()方法并在代码中获取枚举的CSV,但在[Authorize]属性中使用相同的语句时却不能?

c# enums

3
推荐指数
1
解决办法
596
查看次数

通过脚本统一设置播放器设置目标架构

我有自定义的构建脚本,我想在其中添加更多选项。目标架构是其中之一。(我将IL2CPP用作脚本后端-我通过脚本进行设置)

在这里,您可以查看要更改的设置:

根据文档,我可以使用PlayerSettings.SetArchitecture(),但似乎根本不适合我。它只有0-无,1-ARM64,2-通用的选项,我想要全部。所以我尝试使用:

PlayerSettings.SetArchitecture(BuildTargetGroup.Android, unchecked((int)AndroidArchitecture.All));
Run Code Online (Sandbox Code Playgroud)

但它不会改变任何东西,下面的几行也不会改变任何东西:

PlayerSettings.SetArchitecture(BuildTargetGroup.Android, unchecked((int)AndroidArchitecture.ARM64));
PlayerSettings.SetArchitecture(BuildTargetGroup.Android, unchecked((int)AndroidArchitecture.ARMv7));
PlayerSettings.SetArchitecture(BuildTargetGroup.Android, unchecked((int)AndroidArchitecture.X86));
Run Code Online (Sandbox Code Playgroud)

我想知道如何正确地做到这一点?

c# unity-game-engine

3
推荐指数
1
解决办法
2652
查看次数

覆盖枚举

我有一套枚举

public enum enums
{
    enum1,
    enum2,
    enum3,
    enum4,
    enum5
}
Run Code Online (Sandbox Code Playgroud)

在另一个类中我想要两个对象:

var object1 = new enums();//Here I want an enum of enum1,enum2
var object2 = new enums();//Here I want an enum of enum3,enum4,enum5
Run Code Online (Sandbox Code Playgroud)

是否有人知道如何做到这一点?

c# enums

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

a == b和(a&b)== b之间有什么区别?

我读了一些剧本,似乎很难理解.希望有人能解释为什么第一个:

public static bool ContainsDestroyWholeRowColumn(BonusType bt)
    {
        return (bt & BonusType.DestroyWholeRowColumn) 
            == BonusType.DestroyWholeRowColumn;
    }
Run Code Online (Sandbox Code Playgroud)

为什么不写bt.Equal(BonusType.DestroyWholeRowColumn)bt == BonusType.DestroyWhoeRowColumn?第二:

public bool IsSameType(Shape otherShape)
    {
        if (otherShape == null || !(otherShape is Shape))// check otherShape is not null and it is Shape
            throw new ArgumentException("otherShape");

        return string.Compare(this.Type, (otherShape as Shape).Type) == 0;
    }
Run Code Online (Sandbox Code Playgroud)

如果输入法不是正确的类型.我认为它会立即警觉,为什么他们还需要检查对象的类型最后:

//if we are in the middle of the calculations/loops
            //and we have less than 3 matches, return a random one
            if(row >= Constants.Rows / 2 …
Run Code Online (Sandbox Code Playgroud)

c# boolean-operations

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