[英]Activator.CreateInstance can't find the constructor
I got an error in BaseRenderModel<TBaseEntity>
class that says Constructor on type 'Jahan.Nuts.Model.UModel.HomePage' not found.
我在BaseRenderModel<TBaseEntity>
类中BaseRenderModel<TBaseEntity>
错误,该错误BaseRenderModel<TBaseEntity>
Constructor on type 'Jahan.Nuts.Model.UModel.HomePage' not found.
I double checked the code and read some solutions in Internet about it but I couldn't solve my problem. 我仔细检查了代码,并在Internet上阅读了一些解决方案,但是我无法解决问题。 I've used Umbraco 7.3 (ASP.NET MVC) at my project 我在项目中使用了Umbraco 7.3(ASP.NET MVC)
How can I solve this problem? 我怎么解决这个问题?
namespace Jahan.Nuts.Model.UModel.URenderModel
{
public class BaseRenderModel<TBaseEntity> : RenderModel where TBaseEntity : BaseEntity
{
public TBaseEntity Model { get; set; }
public BaseRenderModel(IPublishedContent content, CultureInfo culture) : base(content, culture)
{
object args = new object[] { content, culture };
Model = (TBaseEntity)Activator.CreateInstance(typeof(TBaseEntity), args); //Constructor on type 'Jahan.Nuts.Model.UModel.HomePage' not found.
}
}
}
Class BaseEntity: 类BaseEntity:
public class BaseEntity
{
public BaseEntity()
{
}
public BaseEntity(IPublishedContent content, CultureInfo culture)
{
// some codes
}
public BaseEntity(IPublishedContent content)
{
// some codes
}
}
Class HomePage: 类主页:
public class HomePage : BaseEntity
{
public List<Photo> PhotoList { get; set; }
public HomePage(IPublishedContent content, CultureInfo culture) : base(content, culture)
{
Initialize(content, culture);
}
public HomePage(IPublishedContent content) : base(content)
{
Initialize(content, null);
}
protected HomePage()
{
}
}
Class HomePageController: HomePageController类:
public class HomePageController : RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
BaseRenderModel<HomePage> instance = new BaseRenderModel<HomePage>(model.Content, model.CurrentCulture);
return base.Index(instance);
}
}
There is an error in this line: 这行有错误:
object args = new object[] { content, culture };
When you pass it to Activator.CreateInstance
, it's looking for a constructor which takes one object[]
parameter. 当您将其传递给Activator.CreateInstance
,它正在寻找采用一个object[]
参数的构造函数。 You need args to be an object[]
. 您需要将args用作object[]
。
object[] args = new object[] { content, culture };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.