为什么使用参数的 C# 属性不适用于我的 class 但适用于字符串?
Why C# Attribute using params doesn't work with my class but work with strings?
我有一个在构造函数中使用 params
的属性,我想在参数中将 class 传递给它。
public class FooAttribute: SomeAttribute
{
public FooAttribute(params Foo[] foo)
{
// some code
}
}
public abstract class Foo
{
public Foo(int @int, string name)
{
// code
}
// more code
}
这在使用属性时不起作用
[AuthorizeFeature(FooHelper.X, FooHelper.Y)]
(FooHelper
是一个静态的 class,它使用特定的构造函数参数实例化 Foo
)
我收到以下错误
Error CS0181 Attribute constructor parameter 'foo' has type 'Foo[]', which is not a valid attribute parameter type
但为什么不起作用?在测试时,我将类型 Foo
更改为 string
并且它起作用了,所以我应该能够毫无问题地通过 class 对吧?
或者我无法通过摘要 class?或者不能在构造函数中传递带参数的 class ?或者是否有其他原因导致此错误?我想不通...
Attribute
构造函数的参数必须是常量、类型或它们的一维数组。
您不能使用任何静态只读的东西。因此,您主要限于 string
、数字文字和常量 enums
和 Type
。你甚至不能使用 Decimal
.
解决方法是将属性构造函数的参数作为您可以在属性构造函数的代码中使用的参数来创建自己的实例。
注意 Attributes
的另一个常见陷阱是它们有时会变成 singletons-per-set-of-parameters。您几乎无法控制 Attributes
.
An expression E is an attribute_argument_expression if all of the following statements are true:
- The type of E is an attribute parameter type.
- At compile-time, the value of E can be resolved to one of the following:
- A constant value.
- A System.Type object.
- A one-dimensional array of attribute_argument_expressions.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/attributes
和
The types of positional and named parameters for an attribute class are limited to the attribute parameter types, which are:
- One of the following types: bool, byte, char, double, float, int, long, sbyte, short, string, uint, ulong, ushort.
- 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 (Attribute specification).
- Single-dimensional arrays of the above types.
A constructor argument or public field which does not have one of these types, cannot be used as a positional or named parameter in an attribute specification.
我有一个在构造函数中使用 params
的属性,我想在参数中将 class 传递给它。
public class FooAttribute: SomeAttribute
{
public FooAttribute(params Foo[] foo)
{
// some code
}
}
public abstract class Foo
{
public Foo(int @int, string name)
{
// code
}
// more code
}
这在使用属性时不起作用
[AuthorizeFeature(FooHelper.X, FooHelper.Y)]
(FooHelper
是一个静态的 class,它使用特定的构造函数参数实例化 Foo
)
我收到以下错误
Error CS0181 Attribute constructor parameter 'foo' has type 'Foo[]', which is not a valid attribute parameter type
但为什么不起作用?在测试时,我将类型 Foo
更改为 string
并且它起作用了,所以我应该能够毫无问题地通过 class 对吧?
或者我无法通过摘要 class?或者不能在构造函数中传递带参数的 class ?或者是否有其他原因导致此错误?我想不通...
Attribute
构造函数的参数必须是常量、类型或它们的一维数组。
您不能使用任何静态只读的东西。因此,您主要限于 string
、数字文字和常量 enums
和 Type
。你甚至不能使用 Decimal
.
解决方法是将属性构造函数的参数作为您可以在属性构造函数的代码中使用的参数来创建自己的实例。
注意 Attributes
的另一个常见陷阱是它们有时会变成 singletons-per-set-of-parameters。您几乎无法控制 Attributes
.
An expression E is an attribute_argument_expression if all of the following statements are true:
- The type of E is an attribute parameter type.
- At compile-time, the value of E can be resolved to one of the following:
- A constant value.
- A System.Type object.
- A one-dimensional array of attribute_argument_expressions.
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/attributes
和
The types of positional and named parameters for an attribute class are limited to the attribute parameter types, which are:
- One of the following types: bool, byte, char, double, float, int, long, sbyte, short, string, uint, ulong, ushort.
- 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 (Attribute specification).
- Single-dimensional arrays of the above types. A constructor argument or public field which does not have one of these types, cannot be used as a positional or named parameter in an attribute specification.