我有两个这样定义的自定义属性:
internal class SchemaAttribute : Attribute {
internal SchemaAttribute(string schema) {
Schema = schema;
}
internal string Schema { get; private set; }
}
internal class AttributeAttribute : Attribute {
internal AttributeAttribute(string attribute) {
Attribute = attribute;
}
internal string Attribute { get; private set; }
}
我想将 SchemaAttribute 限制为类,将 AttributeAttribute 限制为属性。
这可行吗?
最佳答案
查看 AttributeUsage和 AttributeTargets .
它看起来像:
[AttributeUsage(AttributeTargets.Class)]
internal class SchemaAttribute : Attribute
{
// Implementation
}
[AttributeUsage(AttributeTargets.Property)]
internal class AttributeAttribute : Attribute
{
// Implementation
}
关于c# - 将属性限制为类或属性是否可行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2884411/