Ever got tired of rewriting the same 10-15 lines of Singleton Object implementation code for each class U use it? Met the flexibility of the inherited static member variables, sparks of the overridable static methods?
''' <summary> ''' Application global instance of T type object ''' </summary> ''' <typeparam name="T"></typeparam> ''' <remarks></remarks> Public Class SingletonObject(Of T) ''' <summary> ''' The Object itself ''' </summary> ''' <remarks></remarks> Private Shared _instance As T ''' <summary> ''' This will come handy by the multi-threaded use ''' </summary> ''' <remarks></remarks> Private Shared _lockObject As Object = New Object() ''' <summary> ''' The constructor is private, don't tinker with it ''' </summary> ''' <remarks></remarks> Private Sub New() End Sub ''' <summary> ''' The method examines if an object is already instaniated, if not ''' synclocks the empty object, and creates a new instance ''' </summary> ''' <returns>T</returns> ''' <remarks>Instead of TypeDescriptor you may use Activator, only a matter of taste</remarks> Public Shared Function GetObject() As T If IsNothing(_instance) Then SyncLock (_lockObject) If _instance Is Nothing Then _ System.ComponentModel.TypeDescriptor.CreateInstance(Nothing, GetType(T), Nothing, Nothing) End SyncLock End If Return _instance End Function End Class
The C# implementation will hopefully come soon by Maci
(ps: Don't try to dump any class into the code which has only parameterized constructor, it may cause an interresting exception by the instaniation...)