views:

229

answers:

3

Here is an easy example:

ASP.NET provides several static classes based around the membership provider. We are forever using them in our pages. We also have a need to extend the base System.Web.UI.Page class. So, one thought we had was to expose the various static classes in our OurCompany.Web.UI.Page implementation.

We cannot use a variable:

System.Web.Security.Roles myRoles;

We cannot expose it as a property:

internal System.Web.Security.Roles Roles { get { return System.Web.Security.Roles; } }

We cannot inherit it:

internal class Roles : System.Web.Security.Roles

Is it possible to expose the static class?

+5  A: 

What do you mean by "expose"? The static classes are already available to whoever wants to use them.

It sounds like you're possibly trying to derive from a class or use a property just to avoid typing as much - that's a bad idea. Inheritance is for specialization, not to make names easier to reach :) Just have a using directive:

using System.Web.Security;

and then you can use Roles.XXX wherever you want in that code.

Jon Skeet
Yah. Thought it would come down to something like this. Thanks!
Keith Barrows
A: 

I don't necessarily know why you'd want to do that, but one thing you could do is create your custom Roles class and clone the methods from the Static one, and within those methods just call the ones from the static class.

zincorp
A: 

Are you trying to extend System.Web.UI.Page with your own extra methods/etc.? If so, why not use inheritance and make your Page inherit from System.Web.UI.Page. Then all you have to do is change your various ASPX pages to use your Page instead of the default. If this is what you want, check out this article for a decent example.

Scott Anderson
Already did this. Global search/replace across the project as well. Recompiled and ran with no errors. Unfortunately, this does not directly address my question.
Keith Barrows