- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在寻找一种自动格式化实体中数据字段的有效方法 - 最好使用属性。
我们需要从数据模型生成一个 PDF 文件。我们希望确保可交付成果的一致性,因此我们希望将一些格式规则应用于某些数据字段(日期、电话号码、邮政编码等)。当然,我可以编写自定义属性和格式化代码,但我不想重新发明轮子。我看到很多使用 DataAnnotations 的 promise (尤其是 DisplayFormat 属性),但我似乎找不到任何使用这些属性的内置类。
我如何在非 UI(即非 MVC)上下文中执行此操作?
这是我们正在寻找的示例:
public class MyDataModel
{
[PhoneNumber]
public string PhoneNumber { get; set; }
public void FormatData()
{
//Invoke some .NET or other method changes the value of PhoneNumber into a desired format, i.e. (888)555-1234 based on its decorations.
}
}
我也对创建数据“ View ”而不是更新原始对象的解决方案持开放态度,即:
MyDataModel formatted = original.FormatData();
任何需要最少代码量的东西都是理想的。
最佳答案
您必须使用反射来读取类型的属性。 This answer提供了一些应该有帮助的扩展方法。这是一个 MVC 问题,但它也可以在 MVC 之外工作:
public static T GetAttribute<T>(this MemberInfo member, bool isRequired)
where T : Attribute
{
var attribute = member.GetCustomAttributes(typeof(T), false).SingleOrDefault();
if (attribute == null && isRequired)
{
throw new ArgumentException(
string.Format(
CultureInfo.InvariantCulture,
"The {0} attribute must be defined on member {1}",
typeof(T).Name,
member.Name));
}
return (T)attribute;
}
public static string GetPropertyDisplayName<T>(Expression<Func<T, object>> propertyExpression)
{
var memberInfo = GetPropertyInformation(propertyExpression.Body);
if (memberInfo == null)
{
throw new ArgumentException(
"No property reference expression was found.",
"propertyExpression");
}
var attr = memberInfo.GetAttribute<DisplayNameAttribute>(false);
if (attr == null)
{
return memberInfo.Name;
}
return attr.DisplayName;
}
public static MemberInfo GetPropertyInformation(Expression propertyExpression)
{
Debug.Assert(propertyExpression != null, "propertyExpression != null");
MemberExpression memberExpr = propertyExpression as MemberExpression;
if (memberExpr == null)
{
UnaryExpression unaryExpr = propertyExpression as UnaryExpression;
if (unaryExpr != null && unaryExpr.NodeType == ExpressionType.Convert)
{
memberExpr = unaryExpr.Operand as MemberExpression;
}
}
if (memberExpr != null && memberExpr.Member.MemberType == MemberTypes.Property)
{
return memberExpr.Member;
}
return null;
}
用法是:
string displayName = ReflectionExtensions.GetPropertyDisplayName<SomeClass>(i => i.SomeProperty);
关于c# - 使用 C#- DisplayFormatAttribute 中的属性格式化字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9265021/
我希望能够在我的 View 模型中标记属性,以便在我的 MVC View 中显示时使用自定义格式进行格式化。 我假设我需要自己的显示格式属性并设置显示格式,但我不确定如何设置此显示格式或在哪里设置。如
我正在使用 Entity Framework v6.1.3(代码优先)和 ASP.NET MVC v5.2.3 我知道我可以像这样在十进制数据类型中使用DisplayFormatAttribute [
我通过以下代码在资源的 DisplayFormat 中设置 NullDisplayText public class LocalizedDisplayFormatAttribute : Display
我正在寻找一种自动格式化实体中数据字段的有效方法 - 最好使用属性。 我们需要从数据模型生成一个 PDF 文件。我们希望确保可交付成果的一致性,因此我们希望将一些格式规则应用于某些数据字段(日期、电话
所以我找到了 DisplayFormatAttribute 类(我猜它只用于 Web 开发?)。它确实会在可维护性方面改进我的项目,因为我有许多小数属性,它们的小数位数似乎一次又一次地改变(客户要求与
我刚刚将一堆 Web 服务转换为 Web API2。现在,当浏览器发送一个空字符串并将我的代码转换为 null 时,我的 C# 代码崩溃了。我研究了全局解决方案,但没有找到适合我的解决方案。 我当然可
属性 DisplayFormatAttribute.ConvertEmptyStringToNull 的默认值为 true。我想将整个站点的默认值设置为 false(或者按类或页面也可以)。有没有办法
我在其他地方读到 DisplayFormat 只是以与 string.Format 相同的方式使用 DataFormatString 。我正在尝试显示一个很长的电话号码;在控制台应用程序中,可以执行以
我是一名优秀的程序员,十分优秀!