本文整理汇总了VB.NET中System.Reflection.PropertyInfo.GetValue方法的典型用法代码示例。如果您正苦于以下问题:VB.NET PropertyInfo.GetValue方法的具体用法?VB.NET PropertyInfo.GetValue怎么用?VB.NET PropertyInfo.GetValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.PropertyInfo
的用法示例。
在下文中一共展示了PropertyInfo.GetValue方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: Planet
' 导入命名空间
Imports System.Reflection
Public Class Planet
Private planetName As String
Private distanceFromEarth As Double
Public Sub New(name As String, distance As Double)
planetName = name
distanceFromEarth = distance
End Sub
Public ReadOnly Property Name As String
Get
Return planetName
End Get
End Property
Public Property Distance As Double
Get
Return distanceFromEarth
End Get
Set
distanceFromEarth = value
End Set
End Property
End Class
Module Example
Public Sub Main()
Dim jupiter As New Planet("Jupiter", 3.65e08)
GetPropertyValues(jupiter)
End Sub
Private Sub GetPropertyValues(obj As Object)
Dim t As Type = obj.GetType()
Console.WriteLine("Type is: {0}", t.Name)
Dim props() As PropertyInfo = t.GetProperties()
Console.WriteLine("Properties (N = {0}):",
props.Length)
For Each prop In props
If prop.GetIndexParameters().Length = 0 Then
Console.WriteLine(" {0} ({1}): {2}", prop.Name,
prop.PropertyType.Name,
prop.GetValue(obj))
Else
Console.WriteLine(" {0} ({1}): <Indexed>", prop.Name,
prop.PropertyType.Name)
End If
Next
End Sub
End Module
输出:
Type is: Planet Properties (N = 2): Name (String): Jupiter Distance (Double): 365000000
示例2: Example
' 导入命名空间
Imports System.Reflection
Module Example
Sub Main()
Dim test As String = "abcdefghijklmnopqrstuvwxyz"
' Get a PropertyInfo object representing the Chars property.
Dim pinfo As PropertyInfo = GetType(String).GetProperty("Chars")
' Show the first, seventh, and last characters.
ShowIndividualCharacters(pinfo, test, { 0, 6, test.Length - 1 })
' Show the complete string.
Console.Write("The entire string: ")
For x As Integer = 0 To test.Length - 1
Console.Write(pinfo.GetValue(test, { x }))
Next
Console.WriteLine()
End Sub
Sub ShowIndividualCharacters(pinfo As PropertyInfo,
value As Object,
ParamArray indexes() As Integer)
For Each index In indexes
Console.WriteLine("Character in position {0,2}: '{1}'",
index, pinfo.GetValue(value, { index }))
Next
Console.WriteLine()
End Sub
End Module
输出:
Character in position 0: 'a' Character in position 6: 'g' Character in position 25: 'z' The entire string: abcdefghijklmnopqrstuvwxyz