简体   繁体   English

Activator.CreateInstance MissingMethodException

[英]Activator.CreateInstance MissingMethodException

I have a class in ac# dll with the following class 我在下面的课程中有一个ac#dll课程

public class RequiredTask : Base.BaseObject
{

    public string Name { get; set; }
    public string Description { get; set; }

    public RequiredTask() 
        : base()
    { }

}

Which inherits from this class 哪个继承自这个类

public class BaseObject : IBaseObject, INotifyPropertyChanged
{
    public DateTime? UpdatedOn { get; set; }
    public string UpdatedBy
    public DateTime? CreatedOn
    public string CreatedBy 

    public BaseObject() { }

}

Then the UI, is a VB.Net Winform, this form is going to be a base form and is generic so it can work with all the types from the c# library, and it has a new button that needs to instanciate the new type of whatever T is and pass it to a form which will be used to edit T. 然后UI是一个VB.Net Winform,这个表单将是一个基本形式,并且是通用的,所以它可以使用c#库中的所有类型,并且它有一个新的按钮,需要实现新的类型无论是什么,并将其传递给将用于编辑T的形式。

this is the form code 这是表单代码

Public Class Search(Of T As Library.Base.BaseObject)

        Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
            If MyBase.OpenFormName <> "" Then App.mfrmMDI.OpenForm(MyBase.OpenFormName, DirectCast(Activator.CreateInstance(GetType(T), New Object()), T))
        End Sub

End Class

But I get this error Constructor on type 'Library.Production.RequiredTask' not found. 但我得到这个错误未找到类型'Library.Production.RequiredTask'的构造函数。 when it reaches 当它到达时

DirectCast(Activator.CreateInstance(GetType(T), New Object()), T)

You're getting the exception because there is no matching constructor on the type that takes a single parameter of type object . 您将获得异常,因为类型上没有匹配的构造函数,它接受类型为object的单个参数。

Change the call to: 将通话更改为:

DirectCast(Activator.CreateInstance(GetType(T)), T)

This should automatically call the default constructor, which is the one you have defined. 这应该自动调用默认构造函数,这是您定义的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Activator.CreateInstance 上的 MissingMethodException - MissingMethodException on Activator.CreateInstance Activator.CreateInstance期间缺少MissingMethodException - MissingMethodException during Activator.CreateInstance Activator.CreateInstance在1个解决方案中引发MissingMethodException,但在另一个解决方案中不引发 - Activator.CreateInstance throws MissingMethodException in 1 solution but not another 如何修复Activator.CreateInstance失败与MissingMethodException“未找到类型的构造函数”? - How to fix Activator.CreateInstance failing with MissingMethodException “Constructor on type not found”? 使用CoreCLR时Activator.CreateInstance抛出&#39;System.MissingMethodException&#39; - Activator.CreateInstance throws 'System.MissingMethodException' when using CoreCLR Activator.CreateInstance - MissingMethodException:找不到类型为“xxx”的构造函数 - Activator.CreateInstance - MissingMethodException: Constructor on type 'xxx' not found Activator.CreateInstance 找不到构造函数 (MissingMethodException) - Activator.CreateInstance can't find the constructor (MissingMethodException) Activator.CreateInstance() - Activator.CreateInstance() Singleton与Activator.CreateInstance - Singleton with Activator.CreateInstance Activator.CreateInstance() 的用法 - Usage of Activator.CreateInstance()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM