开发者

How to restrict class usage

开发者 https://www.devze.com 2023-02-17 23:18 出处:网络
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

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

精彩评论

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