Why can't they code something relevant?
I'm having difficulty deciding why a Microsoft example using attributes at MSDN provides any advantage whatsoever. The example is: Creating Custom Attributes.
(Addendum: As Nick Thissen points out in the comments, it was just the way I was looking at it. So I owe Microsoft an apology on this one. But I decided to leave the rest of this as I originally wrote it because it still makes a good example, although not the one I originally thought it was going to make. One point that is made is the value of more than one set of eyes on a problem. Programming is not a solitary activity.)
MSDN shows how to create a custom "Author" attribute like this:
<System.AttributeUsage( System.AttributeTargets.Class Or System.AttributeTargets.Struct)> Public Class Author Inherits System.Attribute Private name As String Public version As Double Sub New(ByVal authorName As String) name = authorName version = 1.0 End Sub End Class
The attribute is used in this code:
<Author("P. Ackerman", Version:=1.1)> Class SampleClass ' P. Ackerman's code goes here... End Class
Here's my problem. To use this code, a class would have to be coded for every author since the attribute information is hard-coded in the class definition. Somehow, that just doesn't seem very efficient to me. To see if I understood the problem correctly, I coded something with the same capability without using an attribute:
Public Class Author Private name As String Public version As Double Sub New( ByVal authorName As String, Optional ByVal versionNo _ As Double = 1.0) name = authorName If versionNo <> 1.0 Then version = versionNo End If End Sub End Class
Using this class is easier and more flexible and, most importantly, does not require any other class to be coded. You can pass any author or version information to the same class.
Dim theAuthor As New Author("P. Ackerman", 1.1)
Attribute information is saved as part of the metadata in an assembly, but properties are still saved as part of the class instance and I just don't see what reflection (that's the process of saving and retrieving information as part of the metadata is called) buys you.
Perhaps one of you out there can tell me.