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