all groups > asp.net building controls > march 2007 >
You're in the

asp.net building controls

group:

Decorating Derived Page Classes with Custom Attributes


Decorating Derived Page Classes with Custom Attributes Mark Olbert
3/3/2007 7:55:34 AM
asp.net building controls:
I've created a couple of custom class attributes to decorate derived Page classes in my ASPNET application. To access them, I use
the following pattern:

[CustomAttribute(some stuff)]
public partial class DerivedPage : BasePage
{
}

public class BasePage : Page
{
public BasePage()
{
CustomAttribute[] attrs = (CustomAttribute[]) this.GetType().GetCustomAttributes(typeof(CustomAttribute), true);
// there are no attributes!!
}
}

This pattern works fine in every Windows Forms app I've ever written that uses custom attributes.

Is there some aspect of ASPNET that prevents it from working?

Re: Decorating Derived Page Classes with Custom Attributes John Saunders
3/3/2007 12:44:26 PM
[quoted text, click to view]

I don't know why it would work with Windows Forms and not ASP.NET, but
should it work at all? I've always been taught that it's not a good thing to
reference a virtual method within a constructor, and that's just what you're
doing with GetType.

Just as a diagnostic, try calling the same code from a protected method of
the base class and see if it does the same thing.

John

RE: Decorating Derived Page Classes with Custom Attributes wawang NO[at]SPAM online.microsoft.com
3/5/2007 12:00:00 AM
Hi Mark,

I just tried your code and it works correctly by returning the
CustomAttribute applied on DerivedPage. Try to inspect this.GetType().Name
property in your BasePage constructor to see if this.GetType() refers to
DerivedPage (it should).

Can you reproduce the issue easily on your side?

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

Re: Decorating Derived Page Classes with Custom Attributes Mark Olbert
3/5/2007 8:40:49 AM
John,

I've heard of that piece of advice regarding calling virtual methods within constructors. All I can tell you is that "it almost
always works", at least when calling Microsoft-defined methods. I'm not sure I'd be quite so sanguine about calling one of my own...
:)

Re: Decorating Derived Page Classes with Custom Attributes Mark Olbert
3/5/2007 8:44:48 AM
Walter,

I should've mentioned that I tried your diagnostic and found that the correct Type was being reported. Which was part of what was
confusing me.

It turns out the problem was in how I defined the custom attribute class: I set it to Inherited=false, which kept my code from
"seeing" the attributes...which leads me to another question.

I was suprised that Inherited=false caused a problem, because I've done that in many other Windows.Forms based apps and not had a
problem. I am, after all, asking for the custom attributes decorating the Type that they're declared on, so the issue of whether
they're inherited or not should be moot.

But then I recalled vaguely that the ASPNET engine dynamically creates instances of Page classes on the fly, and that made me wonder
if maybe the instances it was creating weren't, technically, the same Type as the Page classes I was declaring. Does that make
sense? If so, it would explain why Inherited=false hid the attributes.

Re: Decorating Derived Page Classes with Custom Attributes wawang NO[at]SPAM online.microsoft.com
3/7/2007 12:00:00 AM
Hi Mark,

You're right that the page class is inherited from your partial class at
run-time:

#ASP.NET 2.0 Internals
http://msdn2.microsoft.com/en-us/library/ms379581(VS.80).aspx

#Understanding Page Inheritance in ASP.NET 2.0
http://west-wind.com/weblog/posts/3016.aspx


Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
AddThis Social Bookmark Button