是否有替代这种逻辑来生成不同的显示字符串?

时间:2017-09-05 12:59:34

标签: c#

假设我们有一个存储三个计数的类,并且有一个返回字符串的显示方法。如果任何计数为零,则不显示它。如果存在Count1或Count2,则仅显示Count3。在单独的行上显示每个计数,其中显示x Count1,x Count2等

    public class Item
    {
        public int Count1 { get; set; }
        public int Count2 { get; set; }
        public int Count3 { get; set; }

        public string ShowCounts()
        {
            string display = string.Empty;

            if (this.Count1 > 0 && this.Count2 > 0)
            {
                display = $"{this.Count1} Count1 \n {this.Count2} Count2";
            }
            if (this.Count1 > 0 && this.Count2 == 0)
            {
                display = $"{this.Count1} Count1";
            }
            if (this.Count1 == 0 && this.Count2 > 0)
            {
                display = $"{this.Count2} Count2";
            }
            if (this.Count3 > 0 && (this.Count1 > 0 || this.Count2 > 0))
            {
                display = $"{display} \n {this.Count3} Count3";
            }

            return display;
        }
   }

4 个答案:

答案 0 :(得分:1)

健壮的方法是添加一个属性(ShowCountAttribute)并具有属性的反射加载并将其称为一天。

事实上,这是一个简单的例子:

[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public class ShowCountAttribute : Attribute
{
    public bool Required { get; set; }
    public int Order { get; set; }
}

然后在ShowCounts

var counts =
    this.GetType()
        .GetProperties()
        .Where(prop => Attribute.IsDefined(prop, typeof(ShowCountAttribute)))
        .Select(prop => new KeyValuePair<string, Tuple<ShowCountAttribute, int>>(prop.Name, new Tuple<ShowCountAttribute, int>((ShowCountAttribute)prop.GetCustomAttributes(typeof(ShowCountAttribute), true).First(), (int)prop.GetValue(this))));

if (counts.Any(x => x.Value.Item2 > 0 && x.Value.Item1.Required))
    return string.Join("\n", counts.Where(kvp => kvp.Value.Item2 > 0).OrderBy(kvp => kvp.Value.Item1.Order).Select(kvp => $"{kvp.Value.Item2} {kvp.Key}"));
else
    return string.Empty;

Name类上定义自己的ShowCustomAttribute也是微不足道的,如果set将覆盖属性名称。我把这个用法留给你了。如果未包含Required计数,则会发送string.Empty

我在这里使用Tuple<,>,因为C#7.0对它们有很好的支持。

答案 1 :(得分:1)

一种可能的方法。 请注意,根据原始代码,如果Count3Count1为正数,我们只会包含Count2

public string ShowCounts()
{
    var results = new List<string>();

    if (this.Count1 > 0)
    {
        results.Add($"{this.Count1} Count1");
    }
    if (this.Count2 > 0)
    {
        results.Add($"{this.Count2} Count2");
    }
    if (results.Any() && this.Count3 > 0)
    {
        results.Add($"{this.Count3} Count3");
    }

    return results.Any() ? string.Join("\n", results) : "";
}

答案 2 :(得分:1)

我知道使用+ =附加固定数量的行比使用StringBuilder更快,但我认为不必一直追加“\ n”使代码更清晰

public string ShowCounts()
{
    var builder = new StringBuilder();  

    bool count1 = this.Count1 > 0;
    bool count2 = this.Count2 > 0;

    if (count1)
        builder.AppendLine($"{this.Count1} Count1");

    if (count2)
        builder.AppendLine($"{this.Count2} Count2");

    if (this.Count3 > 0 && (count1 || count2))
        builder.AppendLine($"{this.Count3} Count3");

    return builder.ToString();
}

答案 3 :(得分:0)

public string ShowCounts()
    {
        string display = string.Empty;

        if (this.Count1 > 0)
        {
            display = $"{this.Count1} Count1";
        }
        if (this.Count2 > 0)
        {
            if( !string.IsNullOrEmpty(display) ) display += "\n";
            display += $"{this.Count2} Count2";
        }
        if (this.Count3 > 0)
        {
            if( !string.IsNullOrEmpty(display) )
            {  // == Count3 > 0 && ( Count1 > 0 || Count2 > 0 )
               display += $"\n{this.Count3} Count3";
            }
        }

        return display;
    }

未经测试,但是如果真的只是关于那些“如果”的话,应该这样做。