Activator.CreateInstance无法找到构造函数

时间:2015-10-31 07:51:51

标签: c# asp.net-mvc reflection umbraco

我在BaseRenderModel<TBaseEntity>课程中收到了Constructor on type 'Jahan.Nuts.Model.UModel.HomePage' not found.

的错误消息

我仔细检查了代码并在网上阅读了一些关于它的解决方案,但我无法解决我的问题。 我在我的项目中使用过Umbraco 7.3(ASP.NET MVC)

我该如何解决这个问题?

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:

public class BaseEntity
{
   public BaseEntity()
    {

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

课程主页:

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:

public class HomePageController : RenderMvcController
 {
    public override ActionResult Index(RenderModel model)
    {
        BaseRenderModel<HomePage> instance = new BaseRenderModel<HomePage>(model.Content, model.CurrentCulture);
        return base.Index(instance);
    }
}

1 个答案:

答案 0 :(得分:3)

此行中有错误:

object args = new object[] { content, culture };

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

object[] args = new object[] { content, culture };