首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Activator.CreateInstance找不到构造函数

Activator.CreateInstance找不到构造函数
EN

Stack Overflow用户
提问于 2015-10-31 07:51:51
回答 1查看 3.7K关注 0票数 0

我在BaseRenderModel<TBaseEntity>类中有一个错误,上面写着Constructor on type 'Jahan.Nuts.Model.UModel.HomePage' not found.

我反复检查了代码,并在互联网上阅读了一些解决方案,但我无法解决问题。我在我的项目中使用了Umbro7.3 (ASP.NET MVC)

我该如何解决这个问题?

代码语言:javascript
复制
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.
    }
}
}

类BaseEntity:

代码语言:javascript
复制
public class BaseEntity
{
   public BaseEntity()
    {

    }
    public BaseEntity(IPublishedContent content, CultureInfo culture)
    {
       // some codes
    }
    public BaseEntity(IPublishedContent content)
    {
       // some codes
    }
}

类HomePage:

代码语言:javascript
复制
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()
    {
    }
}

类HomePageController:

代码语言:javascript
复制
public class HomePageController : RenderMvcController
 {
    public override ActionResult Index(RenderModel model)
    {
        BaseRenderModel<HomePage> instance = new BaseRenderModel<HomePage>(model.Content, model.CurrentCulture);
        return base.Index(instance);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-31 08:04:32

这一行有一个错误:

代码语言:javascript
复制
object args = new object[] { content, culture };

当您将它传递给Activator.CreateInstance时,它正在寻找一个带有一个object[]参数的构造函数。您需要args成为一个object[]

代码语言:javascript
复制
object[] args = new object[] { content, culture };
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33449339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档