[Mono-devel-list] What to do with TypeInitialization in case of exceptions on second attempt to access class ? Singleton pattern

Andriy G. Tereshchenko mono-list at spam.24.odessa.ua
Mon Oct 25 13:07:10 EDT 2004

Hello Mono developers,

While writing failure tests for one of my projects I've found very tricky issue.
This is very common to use singleton pattern using static field initiation. 
But in case if your singleton require some access to external objects - like a configuration settings, file, network or database or
object protected using CRL security - then singleton constructor can fail.

In this case pattern using static properties fail very-very hard:
class Singleton
    Singleton() { throw new ArgumentException("I feel headache. Try again next night"); }
    public static Singleton Instance  { get {  return Singleton.instance; } }
    private static Singleton instance = new Singleton(); 

First of all - user receive System.TypeInitializationException exceptions. 
This is unexpected to user - but documented feature of CLR.
But this email not about this. But about future user actions. 

In case if code will try to access this static property at second (third as so on) attempt - here is most funny things happen. 
In Microsoft implementation -  TypeInitialization thrown for all future access. 
This is somethat good - as all users of incorrectly constructed singleton will be notified about this. 
But in the same time - this can result some parts of singleton code executed more that once and result in side-effects. 

In Java - all future access to non-fully constructed classes result in ClassNotFound exception !!  Not good ;-)

But now then trying to test the same application (Program.cs) on Mono - it looks like Mono decided to implement this in different
All future access to property return null and no exceptions ! Not good. 

As for specification - I think this is undocumented.
CLR Partition I, 8.5.9 Class Type Definition:
4.	If not marked BeforeFieldInit then that type's initializer method is executed at (i.e., is triggered by):
* first access to any static or instance field of that type, or
* first invocation of any static, instance or virtual method of that type
There is nothing about invoking it on second attempt then first attempt failed ;-)

What do you think ? 
Andriy G. Tereshchenko
Odessa, Ukraine
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: Program.cs
Url: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20041025/1534a959/attachment.pl 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Test.java
Type: application/octet-stream
Size: 701 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20041025/1534a959/attachment.obj 

More information about the Mono-devel-list mailing list