C# 单例模式的Class 如何实现反射

xuhuan820706 2008-11-18 08:18:22
同标题,希望大家能帮忙解决一下。

主要的顾虑是单例模式因为构造方法是私有的,不知道能不能使用反射?

如果不能,希望能提示一下有没有能其他代替方法来实现单例模式,并且也能使用反射。

不胜感激。
...全文
579 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
RexZheng 2008-11-19
  • 打赏
  • 举报
回复
可以反射一切内容,包括私有和保护级别
bloodish 2008-11-19
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 xuhuan820706 的回复:]
C#程序:

public partial class MainForm : Form
{
//定义数据源
List <BdSbcwData> list = new List <BdSbcwData>();
private static MainForm Form = null;

public MainForm()
{
//初始化窗体控件
InitializeComponent();
}

public static MainForm GetInstance()
{
if (null == F…
[/Quote]
t.InvokeMember("GetInstance", bf, null, null, null) as Program;

1) as MainForm
2) 第一个参数null,默认就是调用构造函数,不需要注明GetInstance
3) BindingFlag的意义楼主自己去看一下
北斗5188 2008-11-19
  • 打赏
  • 举报
回复
Form form = t.InvokeMember("GetInstance", bf, null, null, null) as Program Form;
wangping_li 2008-11-19
  • 打赏
  • 举报
回复
用System.Activator.CreateInstance
abcyzq 2008-11-19
  • 打赏
  • 举报
回复
学习。
xuhuan820706 2008-11-19
  • 打赏
  • 举报
回复
C#程序:

public partial class MainForm : Form
{
//定义数据源
List<BdSbcwData> list = new List<BdSbcwData>();
private static MainForm Form = null;

public MainForm()
{
//初始化窗体控件
InitializeComponent();
}

public static MainForm GetInstance()
{
if (null == Form)
{
Form = new MainForm();
}
return Form;
}
}

帮忙看看调用以上Class以下的方式有什么错?
BindingFlags bf = BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance;
Type t = Type.GetType(nameSpace + "MainForm");
Form form = t.InvokeMember("GetInstance", bf, null, null, null) as Program;
form.Show();
bloodish 2008-11-19
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xuhuan820706 的回复:]
Program p = t.InvokeMember(null, bf, null, null, null) as Program;

这个Program是那个nameSpace下面的啊

using 。。。。
[/Quote]
你新建一个最简单的控制台程序就知道了...
xuhuan820706 2008-11-19
  • 打赏
  • 举报
回复
Program p = t.InvokeMember(null, bf, null, null, null) as Program;

这个Program是那个nameSpace下面的啊

using 。。。。
云飞何处 2008-11-19
  • 打赏
  • 举报
回复

private Hashtable ht=new Hashtable();
//两个参数分别是程序集的名称(不在执行目录下面需要用全路径名称),窗体类的路径
public Form GetForm(string AssemblyName,string FormName)
{

Assembly am=null;
if(!ht.Contains(AssemblyName))
{
am=Assembly.Load(AssemblyName);

ht.Add(AssemblyName,am);
}
else
{
am=(Assembly)ht[AssemblyName];
}
Type t=am.GetType(FormName);
//t.InvokeMember的第一个参数就是要调用的该类中的方法名或属性名
object obfm=t.InvokeMember("Instance",BindingFlags.Static|BindingFlags.Public|BindingFlags.GetProperty,null,null,new object[]{});
return obfm as Form;

}

窗体代码可能如下:
private static int InstanceCount=0;
private static frmStockInvoice _Instance=null;


public static frmStockInvoice Instance
{
get
{
if (InstanceCount ==0)
{
_Instance =new frmStockInvoice();
InstanceCount ++;
}
return _Instance;
}
}

这样就比较灵活了。可以把窗体的程序集和路径保存在数据库中,方便的绑定到菜单或列表中,在点击的时候调用相应的窗体出来
TrueYi 2008-11-18
  • 打赏
  • 举报
回复
当然能了,但最好使用MVC框架。
y63964632 2008-11-18
  • 打赏
  • 举报
回复
学习下
xuhuan820706 2008-11-18
  • 打赏
  • 举报
回复
wuyq11的方法中那个Class Fo 是单例模式吗?

还有以泛型的方式getInstance似乎不适用,我只有ClassName
bloodish 2008-11-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xuhuan820706 的回复:]
Type t = typeof(Program);

这一句里的Program如果换成 namespaceName + className 不知道可不可以?

是不是只要改成下面这样就可以了?
Type t = Type.GetType(namespaceName + className);

其他的都不用改吗?
[/Quote]

当然可以
其他不需要修改
xuhuan820706 2008-11-18
  • 打赏
  • 举报
回复
Type t = typeof(Program);

这一句里的Program如果换成 namespaceName + className 不知道可不可以?

是不是只要改成下面这样就可以了?
Type t = Type.GetType(namespaceName + className);

其他的都不用改吗?
wuyq11 2008-11-18
  • 打赏
  • 举报
回复
public class Singleton<T>
{
private static T _instance;
public Singleton()
{
}
public static T Instance
{
get
{
if (_instance == null)
{
_instance = (T)System.Activator.CreateInstance(typeof(T));
}
return _instance;
}
}
}
/// 要实现单例的类
public class Fo
{
private int count = 0;
public int Count
{
get
{
count++;
return count;
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Singleton<Fo>.Instance.Count);
Console.WriteLine(Singleton<Fo>.Instance.Count);
Console.WriteLine(Singleton<Fo>Instance.Count);

Console.ReadLine();
}
}
bloodish 2008-11-18
  • 打赏
  • 举报
回复
可以,但是既然要用单例就不该想着创建其他实例
下面是例子

class Program
{
private static Program instance;

static Program()
{
instance = new Program();
}

private Program()
{
//to do
}

public static Program Instance
{
get
{
return instance;
}
}

static void Main(string[] args)
{
BindingFlags bf = BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Instance;
Type t = typeof(Program);
Program p = t.InvokeMember(null, bf, null, null, null) as Program;
Console.WriteLine(Program.Instance.GetHashCode());
Console.WriteLine(p.GetHashCode());
Console.Read();
}
}

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧