(Vb.net)如何在使用Reflection时解决“对象引用未设置为对象的实例”错误

时间:2018-02-27 23:42:52

标签: vb.net reflection show

请帮帮我: 使用Reflection时,如何解决此“对象引用未设置为对象的实例”错误。  我正在创建这个教授语言的“程序”。每个主题都是单独的形式,因为它有许多活动。我有一个显示活动列表的表单。按下名为“Empezar”的按钮时,应显示所选主题(表单):  我试图从包含其名称的字符串中调用其中一个表单。我认为变量已经被声明为一个表单。我也认为该函数有助于变量引用表单,但在调试后我意识到错误就在这里。表格的名称也是正确的。那么您认为问题是什么?

Private Sub Btn_EMPEZAR_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs) Handles Btn_EMPEZAR.Click

  Dim strCreatedFromButton As String = "Frm_The_Greetings" 
  Dim frm As New Form

  frm = DirectCast(CreateObjectInstance(strCreatedFromButton), Form)

  frm.Show() 

End Sub


Public Function CreateObjectInstance(ByVal objectName As String) As Object

  Dim obj As Object

  Try

    If objectName.LastIndexOf(".") = -1 Then
      objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
    End If

    obj = [Assembly].GetEntryAssembly.CreateInstance(objectName) 'The 'problem is here. 
'But I don't know how to correct this 
'because I copied the code 
'from another web page because 
'I'm just trying to learn by myself :/ 
'Why Obj Variable is still equal to "Nothing" after this line? :/

  Catch ex As Exception
  obj = Nothing

  End Try
  Return obj

End Function

0 个答案:

没有答案