在 C# Web 应用程序中设置标志变量

Setting flag variable in c# web application

我已经编写了一段代码,在文本框中使用属性,我正在设置一些这样的值

public string Color
{ 
get{}; 
set
  { 
     SetColor(value);
  } 
} 

private void SetColor(string level)
{
    switch(level.ToLower())
    {
     case "high":
     textbox1.BackColor = System.Drawing.Color.Red;
     break;

     case "medium":
     textbox1.BackColor = System.Drawing.Color.Green;
     break;

     case "low":
     textbox1.BackColor = System.Drawing.Color.Yellow;
     break;
   }
}

但我的主要目的是我需要设置标志,如果标志是高它应该显示红色字体,同样如果标志是中等它应该在标签中显示黄色字体。

public static class EnumExtensions
{
    private static void CheckIsEnum<T>(bool withFlags)
    {
            CheckIsEnum(T) (True);
    }
}

 public static bool IsFlagSet<T>(this T value, T flag)
{
}

设置标志变量时是否需要使用管道符号?我在网上浏览这个,但得到了我意想不到的答案。一个标志布尔值 true 或 false。如果该标志为真,则应根据此启用彩色字体。也请帮助我,我需要从数据库中获取数据。是否可以查看特定值是否具有高、低或中等风险并相应地显示字体

谁能告诉我如何使用枚举将上述代码嵌入到标志中。

  • 您可以定义一个带有或不带有 Description 属性的枚举,但我的代码将适用于这两种属性:(如果您的枚举值由多个单词组成并且您需要,您可以使用 description 属性以 "VeryHigh" 之类的空格作为值来显示它们,但您将其显示为带有空格的 "Very High"。)

    public enum AlertType
    {
        [Description("High")]
        High,
    
        [Description("Medium")]
        Medium,
    
        [Description("Low")]
        Low
    }
    

现在使用一些帮助器方法,例如此帮助器 class 中的方法,您可以使用可以表示 value/description.

的字符串值来获取枚举值
    public static class EnumUtils
    {
        public static string StringValueOf(Enum value)
        {
            var fi = value.GetType().GetField(value.ToString());
            var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes.Length > 0)
            {
                return attributes[0].Description;
            }
            return value.ToString();
        }


        public static T EnumValueOf<T>(string enumStr)
        {
            string[] names = Enum.GetNames(typeof(T));
            foreach (var name in names)
            {
                var enumVal = (Enum) Enum.Parse(typeof (T), name);
                if (string.Compare(StringValueOf(enumVal), enumStr, StringComparison.OrdinalIgnoreCase) == 0)
                {
                    return (T)Enum.Parse(typeof(T), name);
                }
            }

            throw new ArgumentException("The string is not a description or value of the specified enum.");
        }
    }
  • 现在,关于您的问题:

Will I need to Use a pipe symbol while setting flag variable?

不,您不需要它,因为您不能组合枚举中的多个值,例如,您不能说警报同时设置为高和低。当您的枚举值可以组合时,[Flags] 很有用,请参阅 this.

Also please help me on I need to get data from data base. will it be possible and see whether the particular value has high ,low or medium risk and display font accordingly.

在这种情况下,您可以使用辅助方法将枚举字符串转换为枚举值。

编辑

看起来您还需要 AlertType 和 Color 之间的映射,因此您的代码可以是这样的:

// your mappings go here.
private Dictionary<AlertType, Color> _alertColorMappings = new Dictionary<AlertType, Color>()
{
    {AlertType.High, Color.Red},
    {AlertType.Medium, Color.Green},
    {AlertType.Low, Color.Yellow},
};

// Alert property which can be set through a mapping with a combobox
private AlertType _alert;
public AlertType Alert
{
    get
    {
        return _alert;
    }
    set
    {
        if (_alert != value)
        {
            _alert = value;
            textbox1.BackColor = _alertColorMappings[value];
        }
    }
}

希望对您有所帮助。