请教vb.net 的反射问题
我有一个公共变量Str1 ,我想根据“Str1”这个字符串来获取Str1变量的值,
Public Str1 As String = "4141414"
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim ss As String
For I = 0 To 10000
Dim Types As Type = Me.GetType()
Dim fg As FieldInfo = Types.GetField("Str1")
ss = fg.GetValue(Me).ToString()
Next
End Sub
但是,出现一个问题,如果我将
Public Str1 As String = "4141414" 如果放在Module里面作为全局公共变量,上面就报错了“未将对象引用设置到对象的实例。”
我知道可能是me.这块出错了,那我应该怎么改呢?谢谢
[解决办法]
Imports System.Reflection
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim ss As String
For I = 0 To 10000
Dim Types As Type = GetType(a)
Dim fg As FieldInfo = Types.GetField("Str1")
ss = fg.GetValue(Nothing).ToString()
Next
End Sub
End Class
Public Module a
Public Str1 As String = "4141414"
End Module