1
此刻我正在为我的mvc应用程序编写自己的ValidationAttribute。将枚举/编译值作为参数传递给构造函数
我有以下ValidationAttribute代码。
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Parameter, AllowMultiple = false)]
public class RecordAttribute: ValidationAttribute
{
public UniqueDataRecordAttribute(string primaryKeyProperty)
{
}
}
我将我的主属性的字段名称作为字符串传递给我的属性并进行sone验证。 例如: -
[RecordAttribute("CustomerID")]
public class CustomerMetaData
{
}
这对我的作品,但我会遇到问题,如果主键的名称将发生变化。
我创建了一个包含主键属性的枚举。但是,当我试图通过它的编译器告诉我:
的属性参数必须是常量表达式的typeof属性参数类型的表达 或数组创建表达式
我也试过这个方法:Associating enums with strings in C#但效果是一样的。
是否有机会将枚举(或其他编译值)传递给我的属性?
谢谢
为什么会主键变化的名称。这种东西你修好的地方,所以它不会改变....? – 2012-04-26 07:11:43