c# - 将属性限制为类或属性是否可行?

标签 c# vb.net .net-2.0 attributes custom-attributes

我有两个这样定义的自定义属性:

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 限制为属性。

这可行吗?

最佳答案

查看 AttributeUsageAttributeTargets .

它看起来像:

[AttributeUsage(AttributeTargets.Class)]
internal class SchemaAttribute : Attribute
{
    // Implementation
}

[AttributeUsage(AttributeTargets.Property)]
internal class AttributeAttribute : Attribute
{
    // Implementation
}

关于c# - 将属性限制为类或属性是否可行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2884411/

相关文章:

javascript - 从动态 htmlTable 在 vb.net 中获取数据

c# - 为什么 XmlDocument Validate 事件处理程序没有被命中?

.net - 多 View 控件如何处理其 View 状态?

c# - ManagementObject.GetMethodParameters 方法的可能值?

mysql - 如何显示使用 vb.net 加载第一个表单时显示的第一个面板

c# - 如何转换 C# .NET 应用程序以支持多种语言?

.net - 即使所有新发布者都有签名证书,未知发布者警告是否真的会出现?

C# app.config 问题 - ConfigurationErrorsException : Unrecognized element 'setting'

c# - C#中如何枚举COM对象的成员?

C# ffmpeg 无法加载 dll avcodec