T
Tony Tullemans
I am trying to write a subroutine that will examine all
the properties of a form to determine which of those are
SqlCommand objects, and then set the Connection property
of those SqlCommands to a passed-in SqlCommand object. I
can find the properties OK but am unsuccessful in trying
to change their value due to the way I have identified
those SqlCommand objects (as a PropertyInfo). Can anyone
give me a hint (or suggest a better way) as I am totally
bamboolzed? I have tried PropertyInfo.SetValue and
MethodInfo.Invoke but all seem to get the same exception
"Object does not match target type".
I have included some sample code:
Public Sub SetDBConnections(ByVal TargetForm As Form,
ByVal DBConnection As SqlClient.SqlConnection)
Dim myType As Type = TargetForm.GetType
Dim myPropertyInfoList As
System.Reflection.PropertyInfo() = myType.GetProperties
((System.Reflection.BindingFlags.NonPublic Or
System.Reflection.BindingFlags.Public Or
System.Reflection.BindingFlags.Instance))
Dim myPropertyInfo As System.Reflection.PropertyInfo
Try
For Each myPropertyInfo In myPropertyInfoList
If myPropertyInfo.PropertyType Is GetType
(SqlClient.SqlCommand) Then
Dim mySQLCmdType As Type =
myPropertyInfo.PropertyType
Dim mySQLCmdPropertyInfoList As
System.Reflection.PropertyInfo() =
mySQLCmdType.GetProperties
((System.Reflection.BindingFlags.NonPublic Or
System.Reflection.BindingFlags.Public Or
System.Reflection.BindingFlags.Instance))
Dim mySQLCmdPropertyInfo As
System.Reflection.PropertyInfo
For Each mySQLCmdPropertyInfo In
mySQLCmdPropertyInfoList
If mySQLCmdPropertyInfo.PropertyType Is
GetType(SqlClient.SqlConnection) Then
Try
''This technique fails
'Dim min As System.Reflection.MethodInfo
= mySQLCmdPropertyInfo.GetSetMethod
'min.Invoke(Me, New
SqlClient.SqlConnection() {DBConnection})
'So does this technique
If mySQLCmdPropertyInfo.CanWrite Then
mySQLCmdPropertyInfo.SetValue
(myPropertyInfo, DBConnection, Nothing)
End If
Exit For
Catch ex As Exception
MessageBox.Show
(ex.Message, "SetDBConnections Exception",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit For
End Try
End If
Next
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "SetDBConnections",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
the properties of a form to determine which of those are
SqlCommand objects, and then set the Connection property
of those SqlCommands to a passed-in SqlCommand object. I
can find the properties OK but am unsuccessful in trying
to change their value due to the way I have identified
those SqlCommand objects (as a PropertyInfo). Can anyone
give me a hint (or suggest a better way) as I am totally
bamboolzed? I have tried PropertyInfo.SetValue and
MethodInfo.Invoke but all seem to get the same exception
"Object does not match target type".
I have included some sample code:
Public Sub SetDBConnections(ByVal TargetForm As Form,
ByVal DBConnection As SqlClient.SqlConnection)
Dim myType As Type = TargetForm.GetType
Dim myPropertyInfoList As
System.Reflection.PropertyInfo() = myType.GetProperties
((System.Reflection.BindingFlags.NonPublic Or
System.Reflection.BindingFlags.Public Or
System.Reflection.BindingFlags.Instance))
Dim myPropertyInfo As System.Reflection.PropertyInfo
Try
For Each myPropertyInfo In myPropertyInfoList
If myPropertyInfo.PropertyType Is GetType
(SqlClient.SqlCommand) Then
Dim mySQLCmdType As Type =
myPropertyInfo.PropertyType
Dim mySQLCmdPropertyInfoList As
System.Reflection.PropertyInfo() =
mySQLCmdType.GetProperties
((System.Reflection.BindingFlags.NonPublic Or
System.Reflection.BindingFlags.Public Or
System.Reflection.BindingFlags.Instance))
Dim mySQLCmdPropertyInfo As
System.Reflection.PropertyInfo
For Each mySQLCmdPropertyInfo In
mySQLCmdPropertyInfoList
If mySQLCmdPropertyInfo.PropertyType Is
GetType(SqlClient.SqlConnection) Then
Try
''This technique fails
'Dim min As System.Reflection.MethodInfo
= mySQLCmdPropertyInfo.GetSetMethod
'min.Invoke(Me, New
SqlClient.SqlConnection() {DBConnection})
'So does this technique
If mySQLCmdPropertyInfo.CanWrite Then
mySQLCmdPropertyInfo.SetValue
(myPropertyInfo, DBConnection, Nothing)
End If
Exit For
Catch ex As Exception
MessageBox.Show
(ex.Message, "SetDBConnections Exception",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit For
End Try
End If
Next
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "SetDBConnections",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub