1. Computing

MSDN Attributes Example

From , Former About.com GuideJanuary 22, 2011

Follow me on:

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.

Comments
January 23, 2011 at 4:52 am
(1) Nick Thissen says:

Surely the author is the author of a class definition, not of a class instance…? In your case one class could have several instances with several different authors. That doesn’t make any sense. If I write a class and you use an instance of that class, surely I am still the author?

January 23, 2011 at 6:32 am
(2) Andrew says:

Possibly the intention is to show a simplistic example.

IMHO, the intention was not to show how the author’s name might be included in a programme (my preferred spelling), rather it was to show how the attribute can be created, structured, and populated.

Once you have the basics, it is possible to experiment and build on those basics.

Andrew

January 23, 2011 at 10:21 am
(3) Dan Mabbutt says:

Nick …

I see your point. Just as (in AssemblyInfo.vb) the attribute information specifies the company, version and so forth, this class specifies the class definition author. In fact, I did take that the wrong way. (My only excuse at this point is that my mind was focused on Microsoft’s “Pubs” database where “Author” really is as I interpreted it.)

It still makes a good example of two alternatives, so I think I’ll leave the blog as it is. But I suppose I owe Microsoft an apology on this one. Thanks for the comment.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>
Top Related Searches bado enero 22

©2013 About.com. All rights reserved.