I'm creating a library in .Net
I have a static class called DataManager.
public static class NewDataManager
{
public static Soccer Soccer { get; private set; }
static NewDataManager()
{
Soccer = new Classes.Soccer.Soccer();
}
}
Soccer class is like:
public class Soccer
{
public static Mapping.Mapping Mappings { get; private set; }
public Soccer()
{
Mappings = new Mapping.Mapping();
}
}
And Mapping class is just an another public empty class.
When i use this project as a reference in another project, i can access my objects like
NewDataManager.Soccer.Mappings
That's fine, this is what i want. The problem is, i can initialize all classes in referencing project like
var s = new Soccer();
var m = new Mapping();
I want those classes accessible only via my NewDataManager
class, projects using my library should not be able to initialize classes.
How can i restrict that?
To prevent another assembly from instantiating a class, make its constructor internal
:
public class Soccer
{
public static Mapping.Mapping Mappings { get; private set; }
internal Soccer() // <-- instead of 'public'
{
Mappings = new Mapping.Mapping();
}
}
public class Mapping
{
internal Mapping()
{
}
}
(By the way, it looks like you have namespaces named Soccer
and Mapping
. Don't do this.)