Activator.CreateInstance MissingMethodException [英] Activator.CreateInstance MissingMethodException

查看:50
本文介绍了Activator.CreateInstance MissingMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#dll中有一个带有以下类的类

 公共类RequiredTask:Base.BaseObject{公共字符串名称{get;放;}公共字符串说明{放;}公共RequiredTask(): 根据(){}} 

哪个继承自此类

 公共类BaseObject:IBaseObject,INotifyPropertyChanged{公开DateTime?UpdatedOn {get;放;}公共字符串UpdatedBy公开DateTime?创建于公共字符串CreatedBy公共BaseObject(){}} 

然后,UI是一个VB.Net Winform,此表单将成为基本表单,并且是通用表单,因此它可以与c#库中的所有类型一起使用,并且它具有一个新按钮,需要实例化该按钮.T的新类型,并将其传递给将用于编辑T的表单.

这是表格代码

 公共类搜索(T为Library.Base.BaseObject)私有Sub btnNew_Click(ByVal发送者为System.Object,ByVal e为System.EventArgs)处理btnNew.Click如果MyBase.OpenFormName<>"然后App.mfrmMDI.OpenForm(MyBase.OpenFormName,DirectCast(Activator.CreateInstance(GetType(T),New Object()),T))结束子末级 

但是找不到类型为'Library.Production.RequiredTask'的错误构造函数.到达

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

解决方案

您会收到异常,因为该类型上没有匹配的构造函数,该构造函数只接受类型为 object 的单个参数.

将通话更改为:

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

这应该自动调用默认的构造函数,即您定义的构造函数.

I have a class in a c# dll with the following class

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.

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. 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.

Change the call to:

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

This should automatically call the default constructor, which is the one you have defined.

这篇关于Activator.CreateInstance MissingMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆