Using C# 2.0 Generics to achieve a reusable Singleton pattern
来源:互联网 发布:nginx ssl 自签名证书 编辑:程序博客网 时间:2024/05/17 03:36
一般代码:
public sealed class Singleton { Singleton() { } public static Singleton Instance { get { return SingletonCreator.instance; } } class SingletonCreator { // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Nested() { } internal static readonly Singleton instance = new Singleton(); } }用模板实现:
public class SingletonProvider <T> where T:new() { SingletonProvider() {} public static T Instance { get { return SingletonCreator.instance; } } class SingletonCreator { static SingletonCreator() { } internal static readonly T instance = new T(); } }用例:
public class TestClass { private string _createdTimestamp; public TestClass () { _createdTimestamp = DateTime.Now.ToString(); } public void Write() { Debug.WriteLine(_createdTimestamp); } }使用:
SingletonProvider<TestClass>.Instance.Write();
原地址:
0 0
- Using C# 2.0 Generics to achieve a reusable Singleton pattern
- [Java] Design Pattern--ArraryList and LinkedList. A simple to achieve. For novice
- Using 'COMMAND' to achieve page break
- Set a goal, try to achieve it!
- Using the Singleton design pattern
- C# 2.0 - 泛型(Generics)
- A Reusable Class to Draw a Simple Graph
- Introduction to Model View Control (MVC) Pattern using C#
- How to achieve success in a particular field
- [Design Pattern] Generic Singleton Pattern with C#
- A Generic, Reusable Diff Algorithm in C# - II
- Using C# Generics read & update XML file .
- C# - Generics
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- Implementing the Singleton Pattern in C#
- gdb 内存复制到/从文件
- 【转】关于MyEclipse SVN显示资源历史记录乱码问题
- POJ 3744 Scout YYF I 概率dp+矩阵快速幂
- Windows下mysql忘记root密码的解决方法
- 黑马程序员-java 基础之GUI
- Using C# 2.0 Generics to achieve a reusable Singleton pattern
- Android Listview 点击不变色 和 拖动 黑色问题
- Android 四大组件之Activity
- ARP协议全面实战手册1.1.2设置过滤器
- [Swift]基本运算符
- Manage and Create Journals in Oralce Fusion Application
- Oracle 获取字符串中所有中文汉字(含标点符号)
- rpm -q vixie-cron =====> 检测是否安装 crontab 定时执行任务 命令格式与详细例子
- GridBagLayout布局实现简单电子邮件发送界面