使用 Activator.Instance 时未找到类型“”的默认成员
No default member found for type '' when using Activator.Instance
我正在编写一个项目,稍后将使用更多模块对其进行扩展,因此我希望尽可能多的代码是独立的,因此我正在使用各种方法使所有内容保持通用尽可能,并且不需要类型的硬编码列表,classes 等
作为概念证明,我在 class;
中提出了以下代码
Public Class clsAddition
Public Function DoAction(Value1 As String, Value2 As String) As String
Dim dblTmpRet As Double
Dim strTmpRet As String = ""
If IsNumeric(Value1) = False Then strTmpRet += "Argument 'Value1' is not numeric. "
If IsNumeric(Value2) = False Then strTmpRet += "Argument 'Value2' is not numeric. "
If strTmpRet = "" Then
dblTmpRet = CDbl(Value1) + CDbl(Value2)
strTmpRet = CStr(dblTmpRet)
End If
Return strTmpRet
End Function
Private Function IsNumeric(Input As String) As Boolean
Dim blnTmpRet As Boolean
Try
Dim dblTest As Double = CDbl(Input)
blnTmpRet = True
Catch ex As Exception
blnTmpRet = False
End Try
Return blnTmpRet
End Function
Public Sub New()
MyBase.New()
End Sub
End Class
和下面的代码以windows的形式;
Private objObjectClass As Object
Private Sub cmdCreateObject_Click(sender As Object, e As EventArgs) Handles cmdCreateObject.Click
Dim assem As Assembly = GetType(Form1).Assembly
Dim typChosen As Type
Try
typChosen = assem.GetType(Me.comObjectType.SelectedItem.ToString, True)
Catch ex As Exception
typChosen = Nothing
End Try
If Not IsNothing(typChosen) Then
'found it
objObjectClass = Activator.CreateInstance(typChosen, True)()
Else
'didn't find it
Throw New Exception("Unable to locate type in assembly")
End If
End Sub
问题是,在 "Activator.CreateInstance(typChosen, True)()" 行,抛出以下错误
An unhandled exception of type 'System.MissingMemberException'
occurred in Microsoft.VisualBasic.dll Additional information: No
default member found for type 'clsAddition'
目的是让我能够创建 classes 的实例,然后对它们调用已知名称的函数。在此示例中,我将创建另一个名为 "Divide" 的 class,或另一个名为 "Concatinate" 等的 class,但是在实际程序中,这些 classes 都将执行非常不同的动作,具有不同的属性、方法等,其中一个函数(上例中的 "DoAction")共享一个语法和 return 类型。
请帮忙!我已经为此苦苦思索了一段时间!
TIA!
根据 Hans Passant 的评论,这是由于下一行中的额外括号所致;
Activator.CreateInstance(typChosen, True)()
将其更改为 ;
Activator.CreateInstance(typChosen, True)
问题已解决。
我正在编写一个项目,稍后将使用更多模块对其进行扩展,因此我希望尽可能多的代码是独立的,因此我正在使用各种方法使所有内容保持通用尽可能,并且不需要类型的硬编码列表,classes 等
作为概念证明,我在 class;
中提出了以下代码Public Class clsAddition
Public Function DoAction(Value1 As String, Value2 As String) As String
Dim dblTmpRet As Double
Dim strTmpRet As String = ""
If IsNumeric(Value1) = False Then strTmpRet += "Argument 'Value1' is not numeric. "
If IsNumeric(Value2) = False Then strTmpRet += "Argument 'Value2' is not numeric. "
If strTmpRet = "" Then
dblTmpRet = CDbl(Value1) + CDbl(Value2)
strTmpRet = CStr(dblTmpRet)
End If
Return strTmpRet
End Function
Private Function IsNumeric(Input As String) As Boolean
Dim blnTmpRet As Boolean
Try
Dim dblTest As Double = CDbl(Input)
blnTmpRet = True
Catch ex As Exception
blnTmpRet = False
End Try
Return blnTmpRet
End Function
Public Sub New()
MyBase.New()
End Sub
End Class
和下面的代码以windows的形式;
Private objObjectClass As Object
Private Sub cmdCreateObject_Click(sender As Object, e As EventArgs) Handles cmdCreateObject.Click
Dim assem As Assembly = GetType(Form1).Assembly
Dim typChosen As Type
Try
typChosen = assem.GetType(Me.comObjectType.SelectedItem.ToString, True)
Catch ex As Exception
typChosen = Nothing
End Try
If Not IsNothing(typChosen) Then
'found it
objObjectClass = Activator.CreateInstance(typChosen, True)()
Else
'didn't find it
Throw New Exception("Unable to locate type in assembly")
End If
End Sub
问题是,在 "Activator.CreateInstance(typChosen, True)()" 行,抛出以下错误
An unhandled exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll Additional information: No default member found for type 'clsAddition'
目的是让我能够创建 classes 的实例,然后对它们调用已知名称的函数。在此示例中,我将创建另一个名为 "Divide" 的 class,或另一个名为 "Concatinate" 等的 class,但是在实际程序中,这些 classes 都将执行非常不同的动作,具有不同的属性、方法等,其中一个函数(上例中的 "DoAction")共享一个语法和 return 类型。
请帮忙!我已经为此苦苦思索了一段时间!
TIA!
根据 Hans Passant 的评论,这是由于下一行中的额外括号所致;
Activator.CreateInstance(typChosen, True)()
将其更改为 ;
Activator.CreateInstance(typChosen, True)
问题已解决。