How to call singleton

  • Thread starter Thread starter mota
  • Start date Start date
M

mota

Hi,

I have singleton class in C#:

class Logger
{
static Logger lInstance = new Logger();

private Logger() {}

public static Logger GetInstance() { return lInstance; }

}

When I want to access it from another assembly, like Logger l =
Logger.GetInstance(), I get TypeInitializationException ->
NullReferenceException.
Any help?
 
Try this :

class Logger
{
private static Logger lInstance = null;

private Logger() {}

public static Logger GetInstance() {
if ( lInstance == null )
lInstance = new Logger();

return lInstance;
}

}
 
Hmm, more problems arising. Here is exact code:

public class LoggerController
{
private static readonly LoggerController lInstance = new
LoggerController();

private LoggerController() {//some code}

public static LoggerController GetInstance()
{
return lInstance;
}
}

1. I need easy thread safe solution.
2. I need the assembly containing singleton to be database project.
3. Thus static is not allowed in safe mode unless readonly keyword is
specified.
 
Back
Top