C# 템플릿 상속

가끔 사용하게 되는 기능 중에 Template 기능은 항상 헛갈린다.
메모를 해두자
———
 

·미리보기 | 소스복사·
  1. using System;
  2. using System.Reflection;
  3. public class Singleton<T> where T : class
  4. {
  5.     private static object _syncobj = new object();
  6.     private static volatile T _instance = null;
  7.     public static T i{
  8.         get
  9.         {
  10.             if (_instance == null)
  11.             {
  12.                 CreateInstance();
  13.             }
  14.             return _instance;
  15.         }
  16.     }
  17.     private static void CreateInstance()
  18.     {
  19.        lock (_syncobj)
  20.         {
  21.             if (_instance == null)
  22.             {
  23.                 Type t = typeof(T);
  24.                 // Ensure there are no public constructors…
  25.                 ConstructorInfo[] ctors = t.GetConstructors();
  26.                 if (ctors.Length > 0)
  27.                 {
  28.                    throw new InvalidOperationException(String.Format(“{0} has at least one accesible ctor making it impossible to enforce singleton behaviour”, t.Name));
  29.                 }
  30.                 // Create an instance via the private constructor
  31.                 _instance = (T)Activator.CreateInstance(t, true);
  32.             }
  33.         }
  34.     }
  35. }

			

“C# 템플릿 상속”의 26개의 댓글

  1. I just want to mention I am just beginner to blogging and truly savored this web page. More than likely I’m likely to bookmark your blog . You certainly have superb article content. Thanks for sharing with us your web page.

  2. I’m honored to obtain a call from a friend as he identified the important tips shared on your site. Browsing your blog post is a real excellent experience. Many thanks for taking into consideration readers at all like me, and I wish you the best of achievements as being a professional domain.

  3. Oh my benefits! an amazing short article guy. Thanks Nevertheless I am experiencing problem with ur rss. Don?t recognize why Not able to sign up for it. Is there anyone obtaining the same rss problem? Anybody that understands kindly react. Thnkx

  4. There are definitely a great deal of information like that to take into consideration. That is an excellent point to raise. I provide the thoughts over as basic motivation however plainly there are concerns like the one you bring up where one of the most important point will certainly be working in straightforward good faith. I don?t understand if best methods have arised around points like that, but I am sure that your task is clearly recognized as an up for grabs. Both young boys and women feel the impact of simply a moment?s enjoyment, for the rest of their lives.

  5. An interesting discussion deserves remark. I assume that you ought to create extra on this subject, it may not be a forbidden topic but generally people are inadequate to speak on such subjects. To the next. Cheers

  6. After research study a few of the blog posts on your web site currently, and I absolutely like your means of blog writing. I bookmarked it to my bookmark site listing and also will be inspecting back quickly. Pls have a look at my website as well as well as let me know what you believe.

  7. Aw, this was a truly good blog post. In concept I would like to put in writing such as this furthermore? taking time and also real effort to make a very good post? but what can I say? I put things off alot and also by no means seem to get something done.

  8. A motivating discussion is worth comment. I believe that you need to publish more about this issue, it might not be a taboo matter but typically people do not discuss these subjects. To the next! All the best!!

  9. Can I just state what a relief to discover someone that in fact recognizes what theyre discussing on the internet. You certainly recognize just how to bring a problem to light as well as make it important. More individuals need to read this and recognize this side of the tale. I cant think youre not much more preferred because you certainly have the present.

댓글 남기기