views:

55

answers:

2

I have two custom attributes defined like so:

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; }
}

I would like to restrict the SchemaAttribute to classes, and the AttributeAttribute to properties.

Is this doable?

+7  A: 

Check out AttributeUsage and AttributeTargets.

It'll look something like:

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

[AttributeUsage(AttributeTarget.Property)]
internal class AttributeAttribute : Attribute
{
    // Implementation
}
Justin Niessner
+1 - Much better than mine.
Kyle Rozendo
+3  A: 

Look at AttributeTargetAttribute

[AttributeTarget(AttributeTargets.Class)]
internal class SchemaAttribute : Attribute
...

[AttributeTarget(AttributeTargets.Property)]
internal class AttributeAttribute: Attribute
...
Phil Lamb
+1 Thanks for your answer! =)
Will Marcouiller