Ing*_*als 7 c# string-formatting data-annotations displayattribute
想知道这是否可能或具有这种效果的东西.
public class MyModel
{
public string Name { get; set; }
[Display(Name = String.Format("This is [0]'s phone number", Name)]
public string PhoneNumber { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我在谈论一个带有变量的DisplayName,非静态的,可能还基于模型的其他属性.这有可能吗?
这是不可能的,因为为属性参数指定的参数必须是常量值(本能地,因为没有与其他任何内容相关的上下文,并且不一定能够在编译时解析(这是一个要求))。来自 C# 规范 (3.0) \xc2\xa717.2:
\n\n如果以下所有陈述都为真,则表达式E是属性参数表达式:
E是属性参数类型(\xc2\xa717.1.3)。E可以解析为以下之一:\n| 归档时间: |
|
| 查看次数: |
4308 次 |
| 最近记录: |