Jon Peterson
Jon Peterson

Reputation: 813

Custom Attribute For Specifying Display Format

Let me start by saying that I don't know that this specifically requires a custom attribute, but the DisplayFormatAttribute most closely matches the intent I am looking for.


What I Would Like

I would like to be able to specify a string format for properties of a class like so:

public class TestAttribute
{
    [CustomDisplayFormatAttribute(DataFormatString = "{0}")]
    public int MyInt { get; set; }

    [CustomDisplayFormatAttribute(DataFormatString = "{0:0.000}")]
    public float MyFloat { get; set; }

    [CustomDisplayFormatAttribute(DataFormatString = "{0:0.0}")]
    public float MyFloat2 { get; set; }

    [CustomDisplayFormatAttribute(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime MyDateTime { get; set; }
}

...and be able to use it like so:

TestAttribute t = new TestAttribute()
        {
            MyDateTime = DateTime.Now,
            MyFloat = 1.2345678f,
            MyFloat2 = 1.2345678f,
            MyInt = 5
        };
Console.WriteLine(t.MyDateTime.ToFormattedString());
Console.WriteLine(t.MyFloat.ToFormattedString());
Console.WriteLine(t.MyFloat2.ToFormattedString());
Console.WriteLine(t.MyInt.ToFormattedString());


What I Have Done So Far

I have successfully created the custom attribute CustomDisplayFormatAttribute and have applied it to my elements, however I am unable to get that attribute out without knowledge of my TestAttribute class.

My first thought was to use an extension method to handle it, hence the ToFormattedString() function.

That being said, ideally I would be able to call a function like ToFormattedString() and have it handle looking up the display format and applying the value to it.


My Questions

  1. Is this possible using C#
  2. How can I get this (or similar) functionality.

Upvotes: 1

Views: 4422

Answers (1)

Marcel Gosselin
Marcel Gosselin

Reputation: 4716

It is not possible to retrieve the TestAttribute class or its properties when you are in the ToFormattedString() method. An alternative would be to pass the method an extra argument which is an expression to get the property. I've heard that processing Linq expression is expensive, you would need to test if this is true in your case:

public interface IHaveCustomDisplayFormatProperties
{
}

public class TestAttribute : IHaveCustomDisplayFormatProperties
{
    [CustomDisplayFormatAttribute(DataFormatString = "{0}")]
    public int MyInt { get; set; }

    [CustomDisplayFormatAttribute(DataFormatString = "{0:0.000}")]
    public float MyFloat { get; set; }

    [CustomDisplayFormatAttribute(DataFormatString = "{0:0.0}")]
    public float MyFloat2 { get; set; }

    [CustomDisplayFormatAttribute(DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime MyDateTime { get; set; }
}

public static class IHaveCustomDisplayFormatPropertiesExtensions
{
    public static string FormatProperty<T, U>(this T me, Expression<Func<T, U>> property)
        where T : IHaveCustomDisplayFormatProperties
    {
        return null; //TODO: implement
    }
}

which could be used like this:

TestAttribute t = new TestAttribute()
{
    MyDateTime = DateTime.Now,
    MyFloat = 1.2345678f,
    MyFloat2 = 1.2345678f,
    MyInt = 5
};
Console.WriteLine(t.FormatProperty(x => x.MyDateTime));
Console.WriteLine(t.FormatProperty(x => x.MyFloat));
Console.WriteLine(t.FormatProperty(x => x.MyFloat2));
Console.WriteLine(t.FormatProperty(x => x.MyInt));

Upvotes: 2

Related Questions