The problem with this is .NET only allows you to put compile-time constants in attributes. Another option is to inherit from DisplayFormatAttribute
and lookup the display format in the constructor, like so:
SomeClass.cs
public class SomeClass
{
public static string GetDateFormat()
{
// return date format here
}
}
DynamicDisplayFormatAttribute.cs
public class DynamicDisplayFormatAttribute : DisplayFormatAttribute
{
public DynamicDisplayFormatAttribute()
{
DataFormatString = SomeClass.GetDateFormat();
}
}
Then you can use it as so:
[DynamicDisplayFormat(ApplyFormatInEditMode = true)]
public DateTime? CreatedOn { get; set; }
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…