反射破坏单例

class SingLeton
{
private static SingLeton inntleton = null;
private ArrayList stlit = null;
//私有构造函数
private SingLeton()
{ }

public static SingLeton singl()
{
if (inntleton == null)
{
inntleton = new SingLeton(http://www.amjmh.com/v/);

}
return inntleton;
}
}
然后通过反射创建这个单例对象并且对比

//调用私有方法构造函数
var ass = Assembly.Load("TextCord");
var type = ass.GetType("TextCord.SingLeton");
var singletonCreate = Activator.CreateInstance(type, true);
var singleton = SingLeton.singl();

Console.WriteLine(object.ReferenceEquals(singleton, SingLeton.singl()));//true

猜你喜欢

转载自www.cnblogs.com/hyhy904/p/11498528.html