我有两个类
Namespace Business
Public Class Core
Public Shared ReadOnly Property Settings As DBManager.Settings
Get
Return DBManager.Settings.GetSettings
End Get
End Property
End Class
End Namespace
Namespace DBManager
Public Class Settings
Private _Name As String
Public Property Name As String
Get
Return _Name
End Get
Set(ByVal value As String)
_Name = value
End Set
End Property
Private _Title As String
Public Property Title As String
Get
Return _Title
End Get
Set(ByVal value As String)
_Title = value
End Set
End Property
Public Shared Function GetSettings() As Settings
Return New Settings With {.Name = "Website", .Title = "My Product Site"}
End Function
End Class
End Namespace
现在,我希望创建一个DataBound Label
控件,其属性名称为DataProperty
,我可以传递属性名称的完整路径。
Namespace Application.Controls
Public Class ExtendedLabel
Inherits Label
Public Property DataProperty As String
Get
If ViewState("DataProperty") Is Nothing Then
Return String.Empty
Else
Return CStr(ViewState("DataProperty"))
End If
End Get
Set(ByVal value As String)
ViewState("DataProperty") = value
End Set
End Property
Private Sub ExtendedLabel_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not String.IsNullOrEmpty(DataProperty) Then
Me.Text = GetReflectedValue()
End If
End Sub
Private Function GetReflectedValue() As String
//'Need suggestion here
End Function
End Class
End Namespace
用法将是这样的
<cc:ExtendedLabel id="elName" runat="server" DataProperty="Business.Core.Settings.Name" />
请建议使用Reflection来访问此值。
为了澄清,我希望能够访问任何命名空间,静态或实例化的任何类中的任何属性。因此,我不能使用
中给出的声明性格式