Propertyに対するリフレクション
<<準備>>
Dim tUser As New TUserInfo
Dim tList As New TData(Of TUserInfo)
Dim tdUsr As New TDUserInfo(tUser, tList)
(*)TDataはジェネリックな自作クラス
(*)tList.DlListは、List(Of T)
①実行時型情報取得
Dim typ As Type = tList.GetType
Dim typ2 As Type = tList.DList.GetType
Dim typ3 As Type = tList.DList(SINGLE_RECORD).GetType
'---typ.name : Tdata'1
'---typ2.name: List'1
'---typ3.name: TuserInfo
②property有無を確認
IsNothing(tlist.DList(SINGLE_RECORD).GetType.GetProperty("UserId"))
'---False
IsNothing(tlist.DList(SINGLE_RECORD).GetType.GetProperty("UserId2"))
'---True
Dim pf As PropertyInfo = tList.DList(SINGLE_RECORD).GetType.GetProperty("UserId")
Dim pf2 As PropertyInfo = tList.DList(SINGLE_RECORD).GetType.GetProperty("UserId2")
存在しないPropertyを指定してもエラーにはならず、Nothingが帰る。
’---pf2はNothingとなる。
③propertyの値を取得
pf.GetValue(tList.DList(SINGLE_RECORD))
(*)引数には、取得したいpropertyが存在するインスタンスをセット
④propertyの値をセット
pf.SetValue(tList.DList(SINGLE_RECORD),"NewUserId")
(*)引数には、セットしたいpropertyが存在するインスタンスと値をセット
⑤ループで回す場合
Dim val As T = DList(i)
For Each wrk As PropertyInfo In val.GetType().GetProperties()
wrk.Name
wrk.GetValue(val)
Next