You can force this also like this:
public class MyLib
{
public static void InitializeMyLib(...)
{
if (instance != null)
{
throw new InvalidOperationException("Already initialized");
}
instance = new MyLib(...);
}
public static MyLib GetInstance()
{
if (instance == null)
{
throw new InvalidOperationException("Not initialized");
}
return instance;
}
private MyLib(...)
{
...
}
private static MyLib instance;
}
public class LibItem1
{
public LibItem1()
{
MyLib.GetInstance(); // will throw if doesn't exist
}
}
If having a singleton is OK here - your description of this class's behaviour suits this pattern. Of course generic exceptions are used for simplicity, and you can use ordinary create-on-first-use-pattern instead of InitializeMyLib
and GetInstance
separate methods. However, I'm guessing that this is not the case, as then you wouldn't have to prohibit creating Libitem1
's before the initialization.