简体   繁体   English

Activator.CreateInstance找不到构造函数

[英]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.

相关问题 为什么 Activator.CreateInstance&lt;&gt;() 找不到内部无参数构造函数? - Why can't Activator.CreateInstance<>() find an internal parameterless constructor? Activator.CreateInstance 找不到构造函数 (MissingMethodException) - Activator.CreateInstance can't find the constructor (MissingMethodException) Activator.CreateInstance(t,42,args)找不到构造函数 - Activator.CreateInstance(t, 42, args) cannot find constructor 为什么在创建Lazy &lt;&gt;时出现错误“ Activator.CreateInstance找不到构造函数”? - Why do I get an error 'Activator.CreateInstance can't find the constructor' while create Lazy<>? C# Activator.CreateInstance 摘要 class 找不到构造函数 - C# Activator.CreateInstance Abstract class can't find constructor 为什么不能让Activator.CreateInstance找到构造函数? - Why cant Activator.CreateInstance find the constructor? Activator.CreateInstance()-找不到构造函数 - Activator.CreateInstance() - Constructor not found Activator.CreateInstance(string)和Activator.CreateInstance <T> () 区别 - Activator.CreateInstance(string) and Activator.CreateInstance<T>() difference Activator.CreateInstance失败并显示&#39;No parameterless constructor&#39; - Activator.CreateInstance failing with 'No parameterless constructor' Activator.CreateInstance以类为参数调用构造函数 - Activator.CreateInstance calling constructor with class as parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM