C# Attribute that overrides empty properties

时间:2018-08-22 13:44:56

标签: c# .net

Is it possible to add custom attribute on Class, that override all empty string properties? Something like this:

[DefaultValueForEmptyString(Text="N/A")]
public class PersonsDTO 
{
   public string Name { get; set; }
   public string Lastname { get; set; }
   public string Address { get; set; }
}

public class DefaultValueForEmptyString
{
   public static void MapProperties(object Properties, string text) 
   {
       foreach (var property in Properties)
       {
          if(string.IsNullOrEmpty(property))
          {
             property = text // "N/A in this case
          }     
       }
   } 
}

2 个答案:

答案 0 :(得分:4)

To resolve a similar problem ages ago I implemented an extension to handle this :

public static string ValueOrDefault(this string value)
{
    return string.IsNullOrWhiteSpace(value) ? "N/A" : value;
}

Now you can use this on all you string properties :

var person = new PersonsDTO();
//Prints N/A
Console.WriteLine(person.Name.ValueOrDefault());

That's not really impressive but the job is done.

答案 1 :(得分:0)

另一种选择是制作一个自定义字符串类,该类将隐式地从您的自定义类转换为字符串,并从一个字符串隐式转换为您的自定义类。这有点费力,但是制作自定义属性类也是如此。

我制作了.NET小提琴,展示了此here

的示例

.NET小提琴消失时,下面是代码的副本。

using System;

public class DefaultString
{
    private const string _default = "N/A";

    public DefaultString(string normal)
    {
        Value = normal;
    }

    private string _value = _default;
    public string Value
    {
        get
        {
            return _value;
        }
        set
        {
            if (String.IsNullOrEmpty(value))
                _value = _default;
            else
                _value = value;
        }
    }

    public bool IsDefault()
    {
        return Value == _default;

    }

    public override string ToString()
    {
        return Value;
    }

    public static implicit operator string(DefaultString defaultString)
    {
        if (defaultString == null)
            return _default;

        return defaultString.ToString();
    }

    public static implicit operator DefaultString(string normal)
    {
        return new DefaultString(normal);
    }
}

public class Program
{    
    public static void Main()
    {
        DefaultString nullDefault = null;
        DefaultString nullConstructorDefault = new DefaultString(null);
        DefaultString emptyDefault = String.Empty;
        DefaultString emptyConstructorDefault = new DefaultString(String.Empty);
        DefaultString abcDefault = "abc";
        DefaultString abcConstructorDefault = new DefaultString("abcConstructor");

        Console.WriteLine("Default string assigned to null: " + nullDefault);
        Console.WriteLine("Default string constructed with null: " + nullConstructorDefault);
        Console.WriteLine("Default string assigned empty string: " + emptyDefault);
        Console.WriteLine("Default string constructed with empty string: " + emptyConstructorDefault);
        Console.WriteLine("Default string assigned \"abc\": " + abcDefault);
        Console.WriteLine("Default string constructed with \"abcConstructor\": " + abcConstructorDefault);
    }
}

输出应为:

  

分配为null的默认字符串:N / A

     

使用null构造的默认字符串:N / A

     

为默认字符串分配的空字符串:N / A

     

使用空字符串构造的默认字符串:N / A

     

分配给“ abc”的默认字符串:abc

     

使用“ abcConstructor”构造的默认字符串:abcConstructor