如何制作可配置的DisplayFormat属性

时间:2021-01-09 20:32:39

I got a date format like:

我有一个日期格式,如:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
public DateTime? CreatedOn { get; set; }

Now, I want to make every datetime format in my application read from one config file. like:

现在,我想让我的应用程序中的每个日期时间格式从一个配置文件中读取。喜欢:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = SomeClass.GetDateFormat())]
public DateTime? CreatedOn { get; set; }

but this will not compile.

但这不会编译。

How can I do this?

我怎样才能做到这一点?

2 个解决方案

#1


7  

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:

这个问题是.NET只允许你将编译时常量放在属性中。另一种选择是继承DisplayFormatAttribute并在构造函数中查找显示格式,如下所示:

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

#2


0  

I created a string constant in a base class and subclassed my view models off that so I could use it.

我在基类中创建了一个字符串常量,并将我的视图模型子类化,因此我可以使用它。

For example:

public class BaseViewModel : IViewModel
{
    internal const string DateFormat = "dd MMM yyyy";
}

public class MyViewModel : BaseViewModel
{
    [DisplayFormat(ApplyFormatInEditMode = true,
                   DataFormatString = "{0:" + DateFormat + "}")]
    public DateTime? CreatedOn { get; set; }
}

This way I could also reference it with subclassing:

这样我也可以用子类来引用它:

public class AnotherViewModel
{
    [DisplayFormat(ApplyFormatInEditMode = true,
                   DataFormatString = "{0:" + BaseViewModel.DateFormat + "}")]
    public DateTime? CreatedOn { get; set; }
}

#1


7  

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:

这个问题是.NET只允许你将编译时常量放在属性中。另一种选择是继承DisplayFormatAttribute并在构造函数中查找显示格式,如下所示:

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

#2


0  

I created a string constant in a base class and subclassed my view models off that so I could use it.

我在基类中创建了一个字符串常量,并将我的视图模型子类化,因此我可以使用它。

For example:

public class BaseViewModel : IViewModel
{
    internal const string DateFormat = "dd MMM yyyy";
}

public class MyViewModel : BaseViewModel
{
    [DisplayFormat(ApplyFormatInEditMode = true,
                   DataFormatString = "{0:" + DateFormat + "}")]
    public DateTime? CreatedOn { get; set; }
}

This way I could also reference it with subclassing:

这样我也可以用子类来引用它:

public class AnotherViewModel
{
    [DisplayFormat(ApplyFormatInEditMode = true,
                   DataFormatString = "{0:" + BaseViewModel.DateFormat + "}")]
    public DateTime? CreatedOn { get; set; }
}