Imports System.Reflection
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim o As Object = Activator.CreateInstance(Type.GetType("A.a")) '工程名称.类名
Dim MyProperty As PropertyInfo = o.GetType().GetProperty("NAME")
Dim objValue As Object = "Bbb"
MyProperty.SetValue(o, objValue, Nothing)
Dim objReturn() As Object
MsgBox(MyProperty.GetValue(o, objReturn).ToString())
End Sub
End Class
Public Class a
private _name As String
Public _id As Integer
Public Property NAME() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim o As Object = Activator.CreateInstance(Type.GetType("A.a")) '工程名称.类名
Dim MyProperty As PropertyInfo = o.GetType().GetProperty("NAME")
Dim objValue As Object = "Bbb"
MyProperty.SetValue(o, objValue, Nothing)
Dim objReturn() As Object
MsgBox(MyProperty.GetValue(o, objReturn).ToString())
End Sub
End Class
Public Class a
private _name As String
Public _id As Integer
Public Property NAME() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class
本文介绍了一种利用.NET反射机制动态创建对象并设置其属性的方法。通过实例演示了如何使用Activator创建指定类型的实例,并利用PropertyInfo来获取和设置对象的特定属性。

9158

被折叠的 条评论
为什么被折叠?



