• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

对象传递把属性构造函数

用户头像
it1352
帮助1

问题说明

我正在尝试将对象传递给属性构造函数,如下所示:

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.

正确答案

#1

属性中的值仅限于简单类型;例如,基本常量(包括字符串)和 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:

  • 以下类型之一:boolbytechardoublefloatintlongshortstring.
  • 类型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
系列文章
更多 icon
同类精品
更多 icon
继续加载