如何将对象传递给属性构造函数 [英] How to pass objects into an attribute constructor

查看:34
本文介绍了如何将对象传递给属性构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

  • 以下类型之一: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.

      这篇关于如何将对象传递给属性构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆