Metro
Metro

Reputation: 1171

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?

Upvotes: 0

Views: 164

Answers (3)

Scott Nimrod
Scott Nimrod

Reputation: 11595

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.

Upvotes: 0

DaveRead
DaveRead

Reputation: 3413

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.

Upvotes: 0

Diadistis
Diadistis

Reputation: 12174

Don't!!!

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

Upvotes: 1

Related Questions