Restrict custom attribute so that it can be applied only to Specific types in C#?

陌路散爱 提交于 2019-12-17 19:28:01

问题


I have a custom attribute which is applied to class properties and the class itself. Now all the classes that must apply my custom attribute are derived from a single base class.

How can I restrict my Custom Attribute so that it can be applied to only those classes, that must derive from my base class? How do I do this?


回答1:


Darn, I hate it when I prove myself wrong... it works if you define the attribute as a protected nested type of the base-class:

abstract class MyBase {
    [AttributeUsage(AttributeTargets.Property)]
    protected sealed class SpecialAttribute : Attribute {}
}

class ShouldBeValid : MyBase {
    [Special]
    public int Foo { get; set; }
}
class ShouldBeInvalid {
    [Special] // type or namespace not found
    [MyBase.Special] // inaccessible due to protection level
    public int Bar{ get; set; }
}

(original answer)

You cannot do this - at least, not at compile time.

Attributes can be restricted (for exmaple) to "class", "struct", "method", "field", etc - but nothing more granular.

Of course, since attributes do nothing by themselves (ignoring PostSharp), they will be inert on other types (and inert on your types unless you add some code to look at the attributes at runtime).

You could write an FxCop rule, but that seems overkill.

I wonder, however, whether an attribute is the best choice:

Now all the classes that must apply my custom attribute are derived from a single base class.

If they must apply your custom attribute, maybe an abstract (or maybe just virtual) property / method would be a better choice?

abstract class MyBase {
    protected abstract string GetSomeMagicValue {get;}
}
class MyActualType : MyBase {
    protected override string GetSomeMagicValue {get {return "Foo";}}
}



回答2:


I'm not aware of any way to do this with custom attributes.
A better way would be to get the classes to implement an interface with a generic constraint

so

class MyAttribute
{
     // put whatever custom data you want in here
}

interface IAttributeService<T> where T : MyBaseClass
{
     MyAttribute Value{get;}
}

then your base class would do this

class MyBaseClass : IAttributeService<MyBaseClass>
{
     private MyAttribute m_attrib;

     IAttributeService<MyClass>.Value
     {
         get {return m_attrib;}
     }
}

That way the compiler will enforce the constraint at compile time so that only classes
derived from MyBaseClass can implement the interface.
I made it fairly extensible by defining a MyAttribute class but you could of course just return a string or a bool if you needed something simpler

I also made the MyAttribute member private so that derived classes couldn't see it and change it but could make it protected and allow derived classes access if you wanted to.



来源:https://stackoverflow.com/questions/1021190/restrict-custom-attribute-so-that-it-can-be-applied-only-to-specific-types-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!