I’m trying to retrieve a custom Attribute set on a page’s class from inside the MasterPage. Normally to do this I would need to reflect directly on the specific class, but inside the Master page it’s always referred to as the Page type (the parent class).
How do I determine the specific type of the Page property?
Here’s an example of what I’m trying to do:
Dim attrs() As Object = Page.GetType().GetCustomAttributes(GetType(MyCustomAttribute), False)
For Each attr As MyCustomAttribute In attrs
' Do something '
Next
but it only ever returns the attributes attached to the actual Page class.
I’d rather not have to derive a new base type from Page if I can avoid it.
Here is how my class is defined (in the code-behind):
<MyCustom()> _
Partial Class PageClass
Am I defining this in the wrong place?
While
Pagewas the actual type, it was a class representing the ASPX page rather than the partial class my attribute was attached to. To find the partial class I only had to do the following:and used my attribute-finding code on that.
Thanks to TonyB and this SO question for pointing me in the right direction.