locked
Dynamic Reflection of properties (or is this possible?) RRS feed

  • Question

  • User385874101 posted

    Heya guys,


    I'm attempting to develop a generic web config analyzer. Basically it would be a class that analyzes the content of the web.config on app start and crashes if there are invalid or missing keys but also offers strongly typed values rather than strings.


    I've created a specific class in the past that works well

    this is the code, its rather simple.


    Imports System.Web
    Imports System.Configuration.ConfigurationSettings

    Namespace AWInterface.Common
    Public Class AWIBaseConfig
    Private Shared m_current As AWIConfigurationValues

    Public Shared ReadOnly Property Current() As AWIConfigurationValues
    Get
    If m_current Is Nothing Then
    m_current = New AWIConfigurationValues
    End If
    Return m_current
    End Get
    End Property

    Public Function CheckForMissingKeys() As String
    Return Nothing
    End Function

    End Class

    Public Class AWIConfigurationValues
    Public ConnectionString As String
    Public ErrorLogFolder As String
    Public LogJobBankErrors As Boolean
    Public LogBest14Errors As Boolean
    Public LogRRRErrors As Boolean
    Public LogAddLookupErrors As Boolean
    Public WaitJobBank As Integer
    Public WaitBest14 As Integer
    Public WaitRRR As Integer
    Public PostalCode As String
    Public NOC_Code As String
    Public JobBank_WS_URL As String
    Public Best14_WS_URL As String
    Public RRR_WS_URL As String
    Public StreetPerfect_ConfigSettings As String
    Public iHost_WS_Login_URL As String
    Public iHost_WS_Update_URL As String
    Public LogIHostErrors As Boolean
    Public WaitIHost As Integer
    Public iHostUpdate_Username As String
    Public iHostUpdate_Password As String
    Public LogNOCError As Boolean
    Public WaitNOC As Integer
    Public NOCToken As String
    Public NOC_URL As String
    Public UseNOCProxy As Boolean

    Private all_keys As String()
    Private isFound As Boolean

    Public Sub New()
    all_keys = New String() {"ConnectionString", "WSErrorLog", "LogJBError", "LogBest14Error", "LogRRRError" _
    , "LogAddLookupError", "WaitJB", "WaitBest14", "WaitRRR", "PostalCode", "NOC_Code" _
    , "AWInterface.JobBank.JBNocSearch", "AWInterface.Best14.Best14" _
    , "AWInterface.RRR.RRR_targetting", "SP_ConfigSettings" _
    , "AWInterface.IFFS_LoginURL", "AWInterface.IFFS_UpdateURL" _
    , "LogIFFSError", "WaitIFFSUpdate", "IFFSUsername", "IFFSPassword" _
    , "LogNOCError", "WaitNOC", "NOCToken", "AWInterface.NOC_URL", "UseNOCProxy"}

    For Each aKey As String In all_keys
    For Each configKey As String In AppSettings.AllKeys
    isFound = (aKey.ToUpper = configKey.ToUpper)
    If isFound Then Exit For
    Next
    If Not isFound Then
    Throw New ApplicationException(aKey & " is missing from the AWI web.config file")
    End If
    Next

    ConnectionString = AppSettings("ConnectionString")
    ErrorLogFolder = AppSettings("WSErrorLog")
    LogJobBankErrors = Boolean.Parse(AppSettings("LogJBError"))
    LogBest14Errors = Boolean.Parse(AppSettings("LogBest14Error"))
    LogRRRErrors = Boolean.Parse(AppSettings("LogRRRError"))
    LogAddLookupErrors = Boolean.Parse(AppSettings("LogAddLookupError"))
    WaitJobBank = Integer.Parse(AppSettings("WaitJB"))
    WaitBest14 = Integer.Parse(AppSettings("WaitBest14"))
    WaitRRR = Integer.Parse(AppSettings("WaitRRR"))
    PostalCode = AppSettings("PostalCode")
    NOC_Code = AppSettings("NOC_Code")
    JobBank_WS_URL = AppSettings("AWInterface.JobBank.JBNocSearch")
    Best14_WS_URL = AppSettings("AWInterface.Best14.Best14")
    RRR_WS_URL = AppSettings("AWInterface.RRR.RRR_targetting")
    StreetPerfect_ConfigSettings = AppSettings("SP_ConfigSettings")
    iHost_WS_Login_URL = AppSettings("AWInterface.IFFS_LoginURL")
    iHost_WS_Update_URL = AppSettings("AWInterface.IFFS_UpdateURL")
    LogIHostErrors = Boolean.Parse(AppSettings("LogIFFSError"))
    WaitIHost = Integer.Parse(AppSettings("WaitIFFSUpdate"))
    iHostUpdate_Username = AppSettings("IFFSUsername")
    iHostUpdate_Password = AppSettings("IFFSPassword")
    LogNOCError = Boolean.Parse(AppSettings("LogNOCError"))
    WaitNOC = Integer.Parse(AppSettings("WaitNOC"))
    NOCToken = AppSettings("NOCToken")
    NOC_URL = AppSettings("AWInterface.NOC_URL")
    UseNOCProxy = Boolean.Parse(AppSettings("UseNOCProxy"))
    End Sub
    End Class
    End Namespace


    but, what i want to do now is find a way to have a base class that provides basic functionality like key validation and finding missing keys, but i also want to be able to provide a specialized class that offers strongly typed values.


    well, its like what it is now, except that currently, my base class which is a singleton, declares a type of AWIConfigValues. I need to find a way to make that declaration generic. I thought about creating an interface and have the base class declare that except that the interface has to declare the properties to have them strongly typed.

    So what im trying to do is have a generic base class, possibly a generic specialized class and a bunch of custom interfaces , then use reflection in a funky way.

    But i am very new to reflection and i have no idea if what i want to do is possible. Are you guys following me in my delirium ? :)

    Wednesday, March 31, 2010 1:29 PM

Answers

  • User614805505 posted

    http://coding-passion.blogspot.com/2008/12/adding-properties-to-object-dynamically.html


    U may refer to the above link... I'm just complete my own version of similar thing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 31, 2010 11:00 PM