【问题标题】:Custom Attribute For Specifying Display Format用于指定显示格式的自定义属性
【发布时间】:2013-01-04 18:52:30
【问题描述】:

首先让我说我不知道​​这具体需要自定义属性,但DisplayFormatAttribute 最接近我正在寻找的意图。


我想要什么

我希望能够为类的属性指定字符串格式,如下所示:

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

...并且能够像这样使用它:

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


到目前为止我做了什么

我已成功创建自定义属性 CustomDisplayFormatAttribute 并将其应用于我的元素,但是我无法在不知道我的 TestAttribute 类的情况下获取该属性。

我的第一个想法是使用扩展方法来处理它,因此使用了ToFormattedString() 函数。

话虽如此,理想情况下,我可以调用ToFormattedString() 之类的函数并让它处理查找显示格式并将值应用于它。


我的问题
  1. 这可以用 C# 实现吗
  2. 我怎样才能获得这个(或类似的)功能。

【问题讨论】:

    标签: c# string-formatting custom-attributes


    【解决方案1】:

    当您在ToFormattedString() 方法中时,无法检索TestAttribute 类或其属性。另一种方法是向该方法传递一个额外的参数,该参数是获取属性的表达式。我听说处理 Linq 表达式很昂贵,您需要测试您的情况是否属实:

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

    可以这样使用:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      相关资源
      最近更新 更多