你可以看到我正在尝试(但没有)使用以下代码:
protected T GetObject()
{
return new T();
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
编辑:
背景如下.我正在使用标准化方法为所有控制器提供自定义控制器类.所以在上下文中,我需要创建一个控制器类型对象的新实例.所以在撰写本文时,它是这样的:
public class GenericController<T> : Controller
{
...
protected T GetObject()
{
return (T)Activator.CreateInstance(ObjectType);
}
public ActionResult Create()
{
var obj = GetObject()
return View(obj);
}
Run Code Online (Sandbox Code Playgroud)
所以我决定这里的反思最简单.我同意,当然在给出问题的初始陈述时,标记为正确的最合适的答案是使用new()约束的答案.我已经修好了.