tag:blogger.com,1999:blog-2305277518350437028.post2104539737266610683..comments2008-11-20T00:32:43.477+05:30Comments on Cognitive Coding: Hidden Gem: Singleton Factory in C#Shivahttp://www.blogger.com/profile/14027203596298852136noreply@blogger.comBlogger17125tag:blogger.com,1999:blog-2305277518350437028.post-4897891535579760672008-11-20T00:32:00.000+05:302008-11-20T00:32:00.000+05:302008-11-20T00:32:00.000+05:30I am somewhat bothered by the fact that reflection...I am somewhat bothered by the fact that reflection allows this. You have instantiated an object with a private constructor from a class other then the objects class. Isn't that what accessibility is all about?In essence you may have prevented others from instantiating this object with new, but what's to stop from using activator to instantiate it as many times as they want? Kind of defeats the gouranjanoreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-82294990680605602292008-09-05T18:24:00.000+05:302008-09-05T18:24:00.000+05:302008-09-05T18:24:00.000+05:30public static class Singleton(T) where T : new() ...public static class Singleton(T) where T : new() { private readonly static T instance = new T(); static Singleton() { } public static T Instance { get { return instance; } } }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-60185960004804967852008-05-12T10:40:00.000+05:302008-05-12T10:40:00.000+05:302008-05-12T10:40:00.000+05:30using System.Reflection;...Instance = typeof(T).In...using System.Reflection;...Instance = typeof(T).InvokeMember(typeof(T).Name,BindingFlags.CreateInstance |BindingFlags.Instance |BindingFlags.NonPublic|BindingFlags.Public,null, null, null) as T;This is not working in WinCE or handheld application. but Activator.CreateInstance and new is working in WinCE and handheld application. From my perception, This why the previous author use it.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-33943844525988271152008-03-12T15:24:00.000+05:302008-03-12T15:24:00.000+05:302008-03-12T15:24:00.000+05:30@kai: sure that would work@kai: sure that would workShivahttp://www.blogger.com/profile/14027203596298852136noreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-89799143473886089372008-03-12T13:08:00.000+05:302008-03-12T13:08:00.000+05:302008-03-12T13:08:00.000+05:30Another try:using System.Reflection;...Instance = ...Another try:using System.Reflection;...Instance = typeof(T) .InvokeMember(typeof(T).Name, BindingFlags.CreateInstance | BindingFlags.Instance | BindingFlags.NonPublic| BindingFlags.Public, null, null, null) as T;Kainoreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-38544513838428858732008-03-12T12:39:00.000+05:302008-03-12T12:39:00.000+05:302008-03-12T12:39:00.000+05:30@shiva: You're right. What a mistake. The name of ...@shiva: You're right. What a mistake. The name of the method says everything!Kainoreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-78749581612715428212008-03-11T16:54:00.000+05:302008-03-11T16:54:00.000+05:302008-03-11T16:54:00.000+05:30@kai: GetUninitializedObject() will not invoke the...@kai: GetUninitializedObject() will not invoke the constructor on the object that we want to use as a singleton. Clearly, this is not what we want!Shivahttp://www.blogger.com/profile/14027203596298852136noreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-87318935502111955992008-03-11T13:38:00.000+05:302008-03-11T13:38:00.000+05:302008-03-11T13:38:00.000+05:30With Instance = System.Runtime.Serialization.Forma...With Instance = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(T)) as T; you can create an instance with a private constructor. Also it is faster than Activator.CreateInstance.Kainoreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-51467314895812953152008-03-09T01:47:00.000+05:302008-03-09T01:47:00.000+05:302008-03-09T01:47:00.000+05:30Anonymous and Shiva... I see what you mean and yes...Anonymous and Shiva... I see what you mean and yes Shiva's solution is better that way :)Depechiehttp://depblog.weblogs.usnoreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-87326818535903996912008-03-08T08:24:00.000+05:302008-03-08T08:24:00.000+05:302008-03-08T08:24:00.000+05:30Depechie,but then SingletonExample can not have a ...Depechie,but then SingletonExample can not have a private default constructor so how do you guarantee that nobody creates more SingletonExample instances...Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-44619161237067383002008-03-07T18:38:00.000+05:302008-03-07T18:38:00.000+05:302008-03-07T18:38:00.000+05:30Hey Shiva !Nice example, but what Chris o. says is...Hey Shiva !Nice example, but what Chris o. says is possible...We have current code running in our projectpublic abstract class SingletonBase<T> where T : new(){ private static T s_Instance; static SingletonBase() { s_Instance = new T(); } public static T Instance { get { return s_Instance; } }}public class SingletonExample : Depechiehttp://depblog.weblogs.usnoreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-70131101611832915012008-03-07T08:22:00.000+05:302008-03-07T08:22:00.000+05:302008-03-07T08:22:00.000+05:30@anastasiosyal: we can't use new() since the const...@anastasiosyal: we can't use new() since the constructor is private. private constructor is a typical requirement of singleton classes.@Chris O: default(T) will set the instance value as nullShivahttp://www.blogger.com/profile/14027203596298852136noreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-60714465640128281542008-03-07T07:07:00.000+05:302008-03-07T07:07:00.000+05:302008-03-07T07:07:00.000+05:30The solution to adding parameters and providing a ...The solution to adding parameters and providing a simple means to override the construction in arbitrary ways is shown here: http://aabs.wordpress.com/2007/11/21/a-generic-class-factory-for-c-30/AABShttp://aabs.wordpress.com/noreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-28896565496868266612008-03-07T06:14:00.000+05:302008-03-07T06:14:00.000+05:302008-03-07T06:14:00.000+05:30This is simply a generic implementation of this:ht...This is simply a generic implementation of this:http://www.yoda.arachsys.com/csharp/threads/Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-78295993345609825472008-03-07T05:13:00.000+05:302008-03-07T05:13:00.000+05:302008-03-07T05:13:00.000+05:30What about Instance = default(T) instead of Instan...What about Instance = default(T) instead of Instance = (T)Activator.CreateInstance(typeof(T), true)?Chris O.http://seattlesoftware.wordpress.comnoreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-81809548385976198082008-03-07T05:09:00.000+05:302008-03-07T05:09:00.000+05:302008-03-07T05:09:00.000+05:30That looks pretty cool. Just a small recommendatio...That looks pretty cool. Just a small recommendation. Instead of using Activator.CreateInstance I would put a generic restrictor (where T = new()) and simply do new T(). Both the method in your sample and here assume that the singleton class will have a paramerless constructor so a (where T = new()) is a good idea for catching potential errors at compile time. Cheers,AnastasiosyalAnastasiosyalanastasiosyalwww.anastasiosyal.comnoreply@blogger.comtag:blogger.com,1999:blog-2305277518350437028.post-41300603144508397632008-03-07T03:57:00.000+05:302008-03-07T03:57:00.000+05:302008-03-07T03:57:00.000+05:30You should try to implement ISerializable interfac...You should try to implement ISerializable interface also to ensure serializing / deserializing also uses the singleton...Jelle Hissinkhttp://www.blogger.com/profile/02009408566972932715noreply@blogger.com