using System;
using System.Collections.Generic;
namespace HH.WMS.Utils
{
///
/// 创建实例工厂
///
public static class ObjectCreator
{
///
/// 创建实例
///
/// 指定要创建的实例类型
/// 实例
public static T Create()
where T : new()
{
return Create(null);
}
///
/// 创建实例
///
/// 指定要创建的实例类型
/// 初始化实例
/// 实例
public static T Create(Action initial)
where T : new()
{
T t = new T();
if (initial != null)
{
initial(t);
}
return t;
}
///
///
///
/// Specify the type you want to create
///
///
public static object Create(Type t, params object[] parameters)
{
return Activator.CreateInstance(t, parameters);
}
private static Dictionary cacheSingletons = new Dictionary();
private static object lockObj = new object();
///
/// 创建单例
///
/// 创建单例对象的类型
///
public static T CreateSingleton()
where T : class, new()
{
object instance = null;
if (!cacheSingletons.TryGetValue(typeof(T), out instance))
{
lock (lockObj)
{
if (!cacheSingletons.TryGetValue(typeof(T), out instance))
{
instance = new T();
cacheSingletons.Add(typeof(T), instance);
}
}
}
return instance as T;
}
}
}