0

At MSDN an example to write a custom attribute shows the following strange behavior

[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute
{ 
  public virtual string Name
  {
    get {return name;}
  }

  // Define Level property. 
  // This is a read-only attribute. 

  public virtual string Level
  {
    get {return level;}
  }

  // Define Reviewed property. 
  // This is a read/write attribute. 

  public virtual bool Reviewed
  {
    get {return reviewed;}
    set {reviewed = value;}
  }
}

Why all properties are virtual?

flindeberg
  • 4,887
  • 1
  • 24
  • 37
Mahmoud Samy
  • 2,822
  • 7
  • 34
  • 78
  • 4
    They are virtual as per any class with virtual methods / properties, so they can be overridden in a derived class. This in no way means a custom attribute must have virtual properties. – Yuval Itzchakov Aug 25 '14 at 10:46
  • Not necessarily a duplicate, but your question is answered by reading [Can I override a property in c#? How?](http://stackoverflow.com/questions/8447832/can-i-override-a-property-in-c-how). – CodeCaster Aug 25 '14 at 10:49
  • 3
    @CodeCaster I believe the OS is confused due to the fact that the MSDN example only uses virtual properties for custom attributes, not about inheritance and overrides in general. – flindeberg Aug 25 '14 at 11:18

2 Answers2

5

There's section on the particular MSDN article that the OP mentioned that is about attribute "inheritance", i.e. if you have a class with a method that is virtual and is annotated with an attribute, and you add a subclass and override that method, will the subclass method have the attribute applied to it? That's what [AttributeUsage(AttributeTargets.Method, Inherited = false)] is about in the Inherited Property section.

Specifically:

public class MyClass
{
    [MyAttribute]
    [YourAttribute]
    public virtual void MyMethod()
    {
        //...
    }
}

Where YourAttribute is configured with AttributeUsage(AttributeTargets.Method, Inherited = false)] and MyAttribute has the default configuration.

public class MyClass
{
    [MyAttribute]
    [YourAttribute]
    public virtual void MyMethod()
    {
        //...
    }
}

public class YourClass : MyClass
{
    // MyMethod will have MyAttribute but not YourAttribute. 
    public override void MyMethod()
    {
        //...
    }

}

The default value for Inherited is true.

So in short, those properties are virtual in the article to describe this feature.

Rui
  • 4,847
  • 3
  • 29
  • 35
1

I doubt that there is any specific reason to do so, because Attributes work well with or without virtual specifiers on their properties or other members.

public string Name
{
    get {return name;}
}

// Define Level property. 
// This is a read-only attribute. 

public string Level
{
    get {return level;}
}

// Define Reviewed property. 
// This is a read/write attribute. 

public bool Reviewed
{
    get {return reviewed;}
    set {reviewed = value;}
}

Will work just as fine.


But you won't be able to override these methods in derived attribute classes.

For example, this will work:

public class WithVirtualPropAttribute : Attribute
{
    public virtual String Prop {get; set;}
}

public class DerivedFromWithVirtualPropAttribute : WithVirtualPropAttribute 
{
    // Compiles ok
    public virtual String Prop {get{return "0"} set{}}
}

This won't:

public class WithoutVirtualPropAttribute : Attribute
{
    public virtual String Prop {get; set;}
}

public class DerivedFromWithoutVirtualPropAttribute : WithoutVirtualPropAttribute 
{
    // Compilation error !!!!!!!!!!!!!!!!!!!!!!!!!!
    public virtual String Prop {get{return "0";} set{}}
}

It can be a design decision for some specific scenarios, but as overall there is no need do so.

Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53