Torna al Thread

Imports System.ComponentModel Imports System.Threading.Thread Imports System.Reflection Public Class ThreadSafePropertySetter Private _syncInvokeObject As ISynchronizeInvoke Private _PropertyValue As Object = Nothing Delegate Sub setCtrlPropertyDelegate(ByVal ctrl As Object, ByVal propName As String, ByVal propvalue As Object) Delegate Function getCtrlPropertyDelegate(ByVal ctrl As Object, ByVal propName As String) As Object Public Sub New(ByVal syncInvokeObject As ISynchronizeInvoke) Me._syncInvokeObject = syncInvokeObject End Sub Public Sub SetCtrlProperty(Of T)(ByVal ctrl As Object, ByVal propName As String, ByVal propValue As T) SetObjectProperty(ctrl, propName, propValue) End Sub Public Function GetCtrlProperty(ByVal ctrl As Object, ByVal propName As String) As Object Return GetObjectProperty(ctrl, propName) End Function Protected Sub SetObjectProperty(ByVal obj As Object, ByVal propertyName As String, ByVal propertyValue As Object) If _syncInvokeObject.InvokeRequired Then _syncInvokeObject.Invoke(New setCtrlPropertyDelegate(AddressOf SetCtrlProperty), New Object() {obj, propertyName, propertyValue}) Else Dim propInfo As PropertyInfo = obj.GetType.GetProperty(propertyName) If propInfo IsNot Nothing Then If propertyValue Is Nothing Then propInfo.SetValue(obj, Nothing, Nothing) ElseIf propInfo.PropertyType.IsAssignableFrom(propertyValue.GetType) Then propInfo.SetValue(obj, propertyValue, Nothing) End If End If End If End Sub Protected Function GetObjectProperty(ByVal obj As Object, ByVal propertyName As String) As Object If _syncInvokeObject.InvokeRequired Then _syncInvokeObject.Invoke(New getCtrlPropertyDelegate(AddressOf GetCtrlProperty), New Object() {obj, propertyName}) Else Dim propInfoArray() As System.Reflection.PropertyInfo Dim result As New System.Text.StringBuilder 'get array of propertyinfo objects for our targets Type propInfoArray = obj.GetType.GetProperties 'iterate through these propertyinfo objects For Each propInfo As System.Reflection.PropertyInfo In propInfoArray Dim s As Object = Nothing Try If propInfo.GetValue(obj, Nothing) IsNot Nothing Then s = propInfo.GetValue(obj, Nothing).ToString() End If Catch ex As Exception 'niente End Try If propInfo.Name = propertyName Then _PropertyValue = s End If Next End If Return _PropertyValue End Function End Class
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5