开发者

C# - Expose a Static Class inside a normal class?

Here is an easy example:

ASP.NET provides several static classes based around the membership provider. We are forev开发者_C百科er 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?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜