DiPix
DiPix

Reputation: 6083

C# Attribute that overrides empty properties

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
          }     
       }
   } 
}

Upvotes: 1

Views: 1308

Answers (2)

Selmir
Selmir

Reputation: 1206

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 your string properties :

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

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

Upvotes: 5

TJR
TJR

Reputation: 3732

Another option would be to make a custom string class that implicitly converts from your custom class to string and from a string to your custom class. It's a bit heavy handed, but so is making a custom attribute class.

I made a .NET fiddle showing an example of this here

A copy of the code is below in case .NET fiddle goes away.

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);
    }
}

The output should be:

Default string assigned to null: N/A

Default string constructed with null: N/A

Default string assigned empty string: N/A

Default string constructed with empty string: N/A

Default string assigned "abc": abc

Default string constructed with "abcConstructor": abcConstructor

Upvotes: 0

Related Questions