Archie, Thank you for your help. I tried this and it still bombed out. After some uh... "Coding" I came up with this:
Regards!
Public Shared Sub SetPropertiesByIniFile(ByVal targetObject As Object, ByVal iniFileName As String, ByVal iniFileSection As String)
If targetObject Is Nothing Then
Throw New System.ArgumentNullException("targetObject", "SetPropertiesByIniFile null parameter of targetObject")
End If
If Not String.IsNullOrEmpty(iniFileName) Then
Dim p As New IniParser(iniFileName)
Dim settings() As String = p.ListSettings(iniFileSection)
'* Loop thtough all the settings in this section
For index = 0 To settings.Length - 1
Dim pi As System.Reflection.PropertyInfo
pi = targetObject.GetType().GetProperty(settings(index), Reflection.BindingFlags.IgnoreCase Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.Instance)
'* Check if a matching property name exists in the targetObject
If pi IsNot Nothing Then
Dim value As Object = Nothing
If pi.PropertyType.IsEnum Then
If pi.PropertyType.IsEnum Then
Try
'* V3.99y - Added Enum capability
'* Enum type have to be converted from string to the Enum option
value = [Enum].Parse(pi.PropertyType, p.GetSetting(iniFileSection, settings(index)), True)
Catch ex As Exception
System.Windows.Forms.MessageBox.Show("Ini File Error - " & settings(index) & " is an an Enum. Values of " & p.GetSetting(iniFileSection, settings(index)) & " is not a valid option.")
End Try
Else
Dim pt As Type = targetObject.GetType().GetProperty(pi.Name).PropertyType
'* 6-DEC-24 Added so color properties can be used in INI files
If pt Is GetType(System.Drawing.Color) Then
Dim cc As New ColorConverter
value = cc.ConvertFromString(p.GetSetting(iniFileSection, settings(index)))
Else
value = Convert.ChangeType(p.GetSetting(iniFileSection, settings(index)), pt, Globalization.CultureInfo.InvariantCulture)
End If
End If
Else
Dim px As Type = targetObject.GetType().GetProperty(pi.Name).PropertyType
If px Is GetType(System.Drawing.Color) Then
Dim cc As New ColorConverter
value = cc.ConvertFromString(p.GetSetting(iniFileSection, settings(index)))
Else
value = Convert.ChangeType(p.GetSetting(iniFileSection, settings(index)), targetObject.GetType().GetProperty(pi.Name).PropertyType, Globalization.CultureInfo.InvariantCulture)
End If
End If
pi.SetValue(targetObject, value, Nothing)
Else
System.Windows.Forms.MessageBox.Show("Ini File Error - " & settings(index) & " is not a valid property.")
End If
Next
End If
End Sub