출처 : http://www.devpia.com/Maeul/Contents/Detail.aspx?BoardID=45&MAEULNo=18&no=509&ref=509
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim myControl As String = "Label"
Dim i As Integer = 3
FindByName(Of Label)(myControl + i.ToString()).Text = "test"
'(myControl + i.ToString()).FindByName(Of Label)(Me).Text = "test" 'VB.NET은 이런 문법은 안됨. C#은 가능
Dim controlName As String = myControl & i.ToString()
controlName.FindByName(Of Label)(Me).Text = "test"
End Sub
End Class
Public Module ControlSelectorExtension
<System.Runtime.CompilerServices.Extension()>
Public Function FindByName(Of T)(targetClass As Object, name As String) As T
Dim info As Reflection.PropertyInfo = targetClass.GetType().GetProperty(name, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
Return DirectCast(info.GetValue(targetClass, Nothing), T)
End Function
<System.Runtime.CompilerServices.Extension()>
Public Function FindByName(Of T)(name As String, targetClass As Object) As T
Dim info As Reflection.PropertyInfo = targetClass.GetType().GetProperty(name, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic)
Return DirectCast(info.GetValue(targetClass, Nothing), T)
End Function
End Module
'프로그래밍언어 > VB.NET' 카테고리의 다른 글
마우스좌표 가져오기 (0) | 2014.03.28 |
---|---|
문자열로 Sub 호출하기 (0) | 2014.03.23 |
배열내 갯수 출력 (0) | 2014.03.10 |
GetPixel 로 불러온값을 Color 로 변환 (0) | 2014.03.09 |
ProgressBar 사용하기 (BackgroundWorker) (0) | 2014.03.09 |