开发者

How to restrict class usage

Is there a way to enforce a class only to be used in ASP.NET ? So that it can't be referenced in a WinForm app or throw exception when instantiated. Is there some kind of .开发者_如何学编程NET class attribute for this purpose?


Don't!!!

public class WebOnly
{
    public WebOnly()
    {
        if (HttpContext.Current == null)
            throw new Exception("me needs web");
    }
}


The internal access modifier allows classes/methods to only be used within the same assembly.

As long as the deployment of the application / web application is set up so that the class you wish to protect is in a separate assembly to the web application, the class will not be accessible.


Consider using Aspect Oriented Programming techniques.

PostSharp's solution:

Control object visibility – Sometimes internal and private keywords are not enough. Restrict which namespace, assembly or type can reference a member or implement an interface.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜