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
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