对象传递把属性构造函数
问题说明
我正在尝试将对象传递给属性构造函数,如下所示:
I am attempting to pass objects into an Attributes constructor as follows:
[PropertyValidation(new NullOrEmptyValidatorScheme())]
public string Name { get; private set; }
使用此属性构造函数:
public PropertyValidationAttribute(IValidatorScheme validator) {
this._ValidatorScheme = validator;
}
代码无法编译.如上所述,如何将对象传递给属性?
The code won't compile. How can I pass an object into an attribute as above?
是的 NullOrEmptyValidatorScheme 实现了 IValidatorScheme.
Yes NullOrEmptyValidatorScheme implements IValidatorScheme.
错误:错误 CS0182:属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式.
The error: error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.
正确答案
属性中的值仅限于简单类型;例如,基本常量(包括字符串)和 typeof
...你不能使用 new
或其他更复杂的代码.简而言之;你不能这样做.你可以给它类型:
The values into attributes are limited to simple types; for example, basic constants (including strings) and typeof
... you can't use new
or other more complex code. In short; you can't do this. You can give it the type though:
[PropertyValidation(typeof(NullOrEmptyValidatorScheme)]
即PropertyValidation
构造函数采用 Type
,并在代码中使用 Activator.CreateInstance
来创建对象.请注意,理想情况下,您应该只在内部存储字符串 (AssemblyQualifiedName
).
i.e. the PropertyValidation
ctor takes a Type
, and use Activator.CreateInstance
inside the code to create the object. Note that you should ideally just store the string internally (AssemblyQualifiedName
).
来自 ECMA 334v4:
From ECMA 334v4:
§24.1.3 属性参数类型
§24.1.3 Attribute parameter types
位置和命名的类型属性类的参数是仅限于 属性参数类型,它们是:
The types of positional and named parameters for an attribute class are limited to the attribute parameter types, which are:
- 以下类型之一:
bool
、byte
、char
、double
、float
、int
、long
、short
、string
. - 类型
object
. - 类型
System.Type
. - 枚举类型,前提是它具有公共可访问性并且嵌套的类型(如果有)还具有公共可访问性.
- 上述的一维数组类型.
- One of the following types:
bool
,byte
,char
,double
,float
,int
,long
,short
,string
. - The type
object
. - The type
System.Type
. - An enum type, provided it has public accessibility and the types in which it is nested (if any) also have public accessibility.
- Single-dimensional arrays of the above types.
和
§24.2 属性规范
...
一个表达式 E
是一个属性参数表达式如果全部下列说法正确的是:
An expression E
is an attribute-argument-expression if all of the following statements are true:
E
的类型是一个属性参数类型(第 24.1.3 节).- 在编译时,E 的值可以是解决了以下问题之一:
- 一个常数值.
- 指定非泛型的类型表达式(第 14.5.11 节)类型,封闭构造类型(第 25.5.2 节),或未绑定的泛型类型(第 25.5 节).
- 一维数组属性参数表达式.
- The type of
E
is an attribute parameter type (§24.1.3). - At compile-time, the value of E can be resolved to one of the following:
- A constant value.
- A typeof-expression (§14.5.11) specifying a non-generic type, a closed constructed type (§25.5.2), or an unbound generic type (§25.5).
- A one-dimensional array of attribute-argument-expressions.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfigii
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20