PC Review


Reply
Thread Tools Rate Thread

Custom Attributes on Controls

 
 
WFB
Guest
Posts: n/a
 
      11th Feb 2005
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
 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      13th Feb 2005

>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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
WFB
Guest
Posts: n/a
 
      16th Feb 2005
At runtime -- I check in the debugger for
SectionHeader.GetType().GetCustomAttributes(True) and it is not there.

Thanks
Joe

"Mattias Sjögren" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>>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
>
> --
> Mattias Sjögren [MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.



 
Reply With Quote
 
Sean Hederman
Guest
Posts: n/a
 
      16th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> At runtime -- I check in the debugger for
> SectionHeader.GetType().GetCustomAttributes(True) and it is not there.
>
> Thanks
> Joe
>
> "Mattias Sjögren" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>
>>>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
>>
>> --
>> Mattias Sjögren [MVP] mattias @ mvps.org
>> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
>> Please reply only to the newsgroup.

>
>



 
Reply With Quote
 
WFB
Guest
Posts: n/a
 
      17th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:cuvqah$gc4$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> At runtime -- I check in the debugger for
>> SectionHeader.GetType().GetCustomAttributes(True) and it is not there.
>>
>> Thanks
>> Joe
>>
>> "Mattias Sjögren" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>>
>>>>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
>>>
>>> --
>>> Mattias Sjögren [MVP] mattias @ mvps.org
>>> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
>>> Please reply only to the newsgroup.

>>
>>

>
>



 
Reply With Quote
 
Sean Hederman
Guest
Posts: n/a
 
      17th Feb 2005
GetFields with no parameters will not return a protected field. Use:
Me.GetType().GetFields(BindingFlags.GetField Or BindingFlags.Instance Or
BindingFlags.NonPublic)

"WFB" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:cuvqah$gc4$(E-Mail Removed)...
>> 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" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> At runtime -- I check in the debugger for
>>> SectionHeader.GetType().GetCustomAttributes(True) and it is not there.
>>>
>>> Thanks
>>> Joe
>>>
>>> "Mattias Sjögren" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>>>
>>>>>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
>>>>
>>>> --
>>>> Mattias Sjögren [MVP] mattias @ mvps.org
>>>> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
>>>> Please reply only to the newsgroup.
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Controls with non-String Attributes Nathan Sokalski Microsoft ASP .NET 3 28th Jun 2006 10:20 AM
Custom Controls with non-String Attributes Nathan Sokalski Microsoft VB .NET 3 28th Jun 2006 10:20 AM
Custom Attributes on ASP.NET 2.0 User Controls =?Utf-8?B?bWF0cml4MTk3NQ==?= Microsoft ASP .NET 0 13th Feb 2006 12:48 PM
Custom controls attributes Julien Microsoft ASP .NET 4 2nd Feb 2006 12:55 PM
some attributes don't work for custom controls Christian Microsoft VB .NET 1 10th Feb 2004 10:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:48 AM.