Imports System.Reflection
Imports System.Globalization
Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
Handles Button1.Click
Me.ParseControlInstruction("Textbox2.Enabled = False")
Me.ParseControlInstruction("Textbox2.Hide()")
End Sub
Private Sub ParseControlInstruction(ByVal instruction As String)
Dim isProperty As Boolean
Dim isMethod As Boolean
If instruction Like "*[.]*[!(]*[=]*" Then
isProperty = True
ElseIf instruction Like "*[.]*[!=]*[(]*[)]" Then
isMethod = True
Else
Throw New NotImplementedException(String.Format("Cannot recognize the instruction string: {0}", instruction))
End If
' Fix whitespaces.
instruction = instruction.Trim({" "c})
' Fix Namespaces. (THIS IS NOT PROPERLY IMPLEMENTED)
If instruction.StartsWith("Me.", StringComparison.OrdinalIgnoreCase) Then
instruction = instruction.Remove(0, "Me.".Length)
End If
Dim ctrlName As String = instruction.Substring(0, instruction.IndexOf("."c))
If isProperty Then
Dim propName As String = instruction.Remove(0, ctrlName.Length + 1)
propName = propName.Substring(0, propName.IndexOf(" "c))
Dim value As Object = instruction.Substring(instruction.IndexOf("="c) + 1).Trim({" "c})
Me.SetControlProperty(ctrlName, propName, value, MyBase.Controls, searchChildrens:=True)
ElseIf isMethod Then
Dim methodName As String = instruction.Remove(0, ctrlName.Length + 1).TrimEnd({"("c, ")"c})
Dim methodParamsStr As String = instruction.Substring(instruction.IndexOf("("c) + 1)
methodParamsStr = methodParamsStr.Substring(0, methodParamsStr.LastIndexOf(")"c))
Dim methodParams As IEnumerable(Of Object) = methodParamsStr.Split({", "}, StringSplitOptions.RemoveEmptyEntries)
Me.CallControlMethod(ctrlName, methodName, methodParams, MyBase.Controls, searchChildrens:=True)
End If
End Sub
Private Sub SetControlProperty(ByVal ctrlName As String,
ByVal propName As String,
ByVal value As Object,
ByVal controlCollection As Control.ControlCollection,
Optional ByVal searchChildrens As Boolean = False,
Optional ByVal searchFlags As BindingFlags =
BindingFlags.IgnoreCase Or
BindingFlags.Instance Or
BindingFlags.Public Or
BindingFlags.NonPublic)
Dim ctrl As Control = controlCollection.Find(ctrlName, searchChildrens).FirstOrDefault
Dim prop As PropertyInfo
Select Case ctrl Is Nothing
Case False
Try
prop = ctrl.GetType().GetProperty(propName, searchFlags)
Catch ex As AmbiguousMatchException
Throw New AmbiguousMatchException(String.Format("More than one property found with the same name: {0}", propName))
Exit Sub
End Try
Select Case prop Is Nothing
Case False
Try
prop.SetValue(ctrl, Convert.ChangeType(value, prop.PropertyType), Nothing)
Catch ex As Exception
Throw
End Try
Case Else
Throw New NullReferenceException(String.Format("Property not found by name: {0}", propName))
End Select ' prop Is Nothing
Case Else
Throw New NullReferenceException(String.Format("Control not found by name: {0}", ctrlName))
End Select ' ctrl Is Nothing
End Sub
Private Sub CallControlMethod(ByVal ctrlName As String,
ByVal methodName As String,
ByVal methodParams As IEnumerable(Of Object),
ByVal controlCollection As Control.ControlCollection,
Optional ByVal searchChildrens As Boolean = False,
Optional ByVal searchFlags As BindingFlags =
BindingFlags.IgnoreCase Or
BindingFlags.Instance Or
BindingFlags.Public Or
BindingFlags.NonPublic)
Dim ctrl As Control = controlCollection.Find(ctrlName, searchChildrens).FirstOrDefault
Dim method As MethodInfo
Select Case ctrl Is Nothing
Case False
Try
method = ctrl.GetType().GetMethod(methodName, searchFlags)
Catch ex As AmbiguousMatchException
Throw New AmbiguousMatchException(String.Format("More than one method found with the same name: {0}", methodName))
Exit Sub
End Try
Select Case method Is Nothing
Case False
Try
method.Invoke(ctrl, searchFlags, Nothing, methodParams.ToArray, CultureInfo.InvariantCulture)
Catch ex As Exception
Throw
End Try
Case Else
Throw New NullReferenceException(String.Format("Method not found by name: {0}", methodName))
End Select ' prop Is Nothing
Case Else
Throw New NullReferenceException(String.Format("Control not found by name: {0}", ctrlName))
End Select ' ctrl Is Nothing
End Sub
End Class