使用Activator:
//
// Summary:
// Creates an instance of the specified type using that type's default constructor.
//
// Parameters:
// type:
// The type of object to create.
//
// nonPublic:
// true if a public or nonpublic default constructor can match; false if only
// a public default constructor can match.
//
// Returns:
// A reference to the newly created object.
public static object CreateInstance(Type type, bool nonPublic);
实例:
public class Singleton<T> where T : class
{
private static T singleton;
public static T Instance
{
get
{
if (Singleton<T>.singleton == default(T))
{
try
{
Singleton<T>.singleton = (T)Activator.CreateInstance(typeof(T), true);
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}
}
return Singleton<T>.singleton;
}
}
}