Custom Attributes on Controls

W

WFB

Hi,

Is it possible to use custom attributes on controls? For example,

If I have the following attribute defined

<AttributeUsage(AttributeTargets.All)> Public Class MyAttribute : Inherits Attribute
Public Message As String

Sub New(ByVal msg As String)
Message = msg
End Sub
End Class

I can "tag" a class with it: <MyAttribute("Hello")> Public Class MyPage : Inherits BasePage
And then in the debugger at run time I can see it using Instance.GetType().GetCustomAttributes(true). Howevr I can not see the attribute if i "tag" a control with it, such as; <MyAttribute("hello")> Protected WithEvents SectionHeader As XYZ.SalesApp.Controls.WebControls.TitleBar, I can not see the attribute.

Is there a reason for this, or a way to work around it?

Thanks
 
M

Mattias Sjögren

Howevr I can not see the attribute if i "tag" a control with it, such as; <MyAttribute("hello")> Protected WithEvents SectionHeader As XYZ.SalesApp.Controls.WebControls.TitleBar, I can not see the attribute.


Where are you looking for it?



Mattias
 
W

WFB

At runtime -- I check in the debugger for
SectionHeader.GetType().GetCustomAttributes(True) and it is not there.

Thanks
Joe
 
S

Sean Hederman

That code is not tagging a control, it's tagging a field inside another
object. GetType is returning the control's type, which does not have an
attribute on it. Consider the following:

<FirstAttribute("Hello")> Public Class MyControl
End Class

Public Class MyContainer
<SecondAttribute("Hello2")> Protected WithEvents SectionHeader As
TitleBar
End Class

Now, if you used SectionHeader.GetType().GetCustomAttributes(True), it would
return FirstAttribute, not SecondAttribute, since FirstAttribute is defined
on the MyControl type. If, however you got a FieldInfo object pointing to
the SectionHeader field inside the MyContainerType, it's GetCustomAttributes
would return SecondAttribute.

WFB said:
At runtime -- I check in the debugger for
SectionHeader.GetType().GetCustomAttributes(True) and it is not there.

Thanks
Joe
 
W

WFB

Sean,

Im not sure I follow. If I have

Public Class MyClass
<MyCustomattribute()> Protected WithEvents Ctrl as MyLibrary.MyControl

Sub New()
Dim s as String
End Sub

End Class

Then (in the bebugger) during the constructor try Me.GetType().GetFields(),
no results are returned. Can you let me know what Im doing wrong?

Thanks a lot for your help.

Joe

Sean Hederman said:
That code is not tagging a control, it's tagging a field inside another
object. GetType is returning the control's type, which does not have an
attribute on it. Consider the following:

<FirstAttribute("Hello")> Public Class MyControl
End Class

Public Class MyContainer
<SecondAttribute("Hello2")> Protected WithEvents SectionHeader As
TitleBar
End Class

Now, if you used SectionHeader.GetType().GetCustomAttributes(True), it
would return FirstAttribute, not SecondAttribute, since FirstAttribute is
defined on the MyControl type. If, however you got a FieldInfo object
pointing to the SectionHeader field inside the MyContainerType, it's
GetCustomAttributes would return SecondAttribute.
 
S

Sean Hederman

GetFields with no parameters will not return a protected field. Use:
Me.GetType().GetFields(BindingFlags.GetField Or BindingFlags.Instance Or
BindingFlags.NonPublic)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top