我想创建一个通用的工厂模式的机制。我可以强迫后代拥有无参数的构造函数吗?
工厂将是这样的:
public class APlugin<ActionType> where ActionType : IAction
{
// create a new action. Note: ActionType should contain
// an empty constructor
public ActionType CreateAction()
{
return Activator.CreateInstance<ActionType>();
}
}
IAction的后代可能隐藏参数的构造函数,这将导致工厂失败。
给它新的()的约束,所以你可以只使用构造,新的操作类型()。但在插件场景中不一定有用。 – 2011-12-26 17:43:52
当你说“空构造函数”时,你的意思是一个不带参数的构造函数吗? – Gabe 2011-12-26 18:04:43
是@加贝。确切地说, – Odys 2011-12-26 18:19:42