码迷,mamicode.com
首页 > 其他好文 > 详细

反射破坏单例

时间:2019-09-10 18:04:21      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:cti   tco   ons   object   public   私有构造函数   对比   type   als   

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

 

反射破坏单例

标签:cti   tco   ons   object   public   私有构造函数   对比   type   als   

原文地址:https://www.cnblogs.com/hyhy904/p/11498528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!