1

How should I be a) defining Custom Attributes and b) getting the said custom attribute assignment in the following scenario?

Scenario: We would like to define a custom attribute (custAtrib1) to be used by inherited class (myClassFoo of a base class (myBase). Then the base class will retrieve the custom attributes assigned to the inherited instance, then perform some operations.

Problem: Whenever GetCustomAttribute is called in the base class against the inherited class, GetCustomAttibutes method only returns a single result (System.Runtime.CompilerServices.CompilerGlobalScopeAttribute).

Here is how the attributes/classes are defined:

The attribute : (file: myFoo.vb)

'-----------------------------------------------------------------
Namespace Foo.CustomAttributes

<System.AttributeUsage(AttributeTargets.Class, AllowMultiple:=True, inherited:=False)> _
Public Class custAttrib1
    Inherits System.Attribute

    Public Property myAttributeInto as String
End Namespace
'-----------------------------------------------------------------

Base Class: (file: myBar.vb)

'-----------------------------------------------------------------
Namespace Foo.Bar
Public Class myBase

    Private Sub someCoolCode()
        Dim myInstanceType as Type = me.GetType()
        Dim custAttribs as Object() = myInstanceType.GetCustomAttributes(False)

        '-- at this time, only content of custAttribs array is System.Runtime.CompilerServices.CompilerGlobalScopeAttribute)
    End Sub

End Class
End Namespace
'-----------------------------------------------------------------

Inherited Class: (file: myBar2.vb)

'-----------------------------------------------------------------
Namespace Foo.Bar
<Foo.CustomAttributes.custAttrib1(myAttributeInfo:="Coding if fun")> _
Public Class myClassFoo 
      '-- other cool stuff goes there
    Public Sub inheritedMethod()
    End Sub
End Class
End Namespace
'-----------------------------------------------------------------

Thank you for your help

MonkeyPen
  • 23
  • 4
  • your base class isnt decorated with the attribute, so it wont be there for `Me`. your `myclassFoo` which does have the attribute doesnt inherit from mybase. if you add a constructor to the attribute, you can simplify setting the value of the property. – Ňɏssa Pøngjǣrdenlarp Aug 04 '15 at 00:03
  • As an aside, your naming convention for your attribute does not follow best practices. Consider naming your attribute class as follows: `Public Class CustAttrib1Attribute` Note the word `Attribute` at the end is recommended. You can leave that off when using the attribute. Also, class names should be capitalized. – Chris Dunaway Aug 04 '15 at 13:43
  • Thank you for the recommendations! – MonkeyPen Aug 04 '15 at 18:16

1 Answers1

0

The only problem in your code is that you are not inheriting from the base class. Otherwise, it works correctly.

Here is the sample I used to test, including the inheritance and correcting of typos:

Public Class Form1

    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Dim oClass = New myClassFoo
        oClass.someCoolCode()

    End Sub

End Class

<System.AttributeUsage(AttributeTargets.Class, AllowMultiple:=True, inherited:=False)> _
Public Class custAttrib1
    Inherits System.Attribute

    Public Property myAttributeInfo As String
End Class

Public Class MyBaseClass

    Public Sub someCoolCode()
        Dim myInstanceType As Type = Me.GetType()
        Dim custAttribs As Object() = myInstanceType.GetCustomAttributes(False)

        '-- at this time, only content of custAttribs array is System.Runtime.CompilerServices.CompilerGlobalScopeAttribute)

        Debug.WriteLine(DirectCast(custAttribs(0), custAttrib1).myAttributeInfo)
    End Sub

End Class

<custAttrib1(myAttributeInfo:="Coding is fun")> _
Public Class myClassFoo
    Inherits MyBaseClass

    '-- other cool stuff goes there
    Public Sub inheritedMethod()
    End Sub
End Class
competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • My other issue was that the the myBaseClass was inheriting from System.Web.UI.Page. I was unaware that the instance type of ASP.NET pages are not the same as the class-definitions in the associated *.asp.vb file. Instead I needed look at the "BaseType" of the page, e.g., me.GetType().BaseType. Once I had the type-reference to the proper type, I was able to get the necessary attributes listing. – MonkeyPen Aug 04 '15 at 18:19