Reputation: 813
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.
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());
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.
Upvotes: 1
Views: 4422
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