Vb.net Dynamic Properties on inherited objects

592 views Asked by At

I cannot get this to work! I want to be able to create dynamic properties on derived classes that inherit from a base class that inherits from system.dynamic.dynamicobject?

I have created a VB.net abstract class which I want to inherit from with dynamic properties on the instances that inherit from this abstract class. The base class reads an XML document and transposes the elements and any attributes into a property list Dictionary.

The idea is that when the derived class is instantiated all of the items listed in the abstract class propertyList are exposed as public properties. From the code I have listed below, if the property id is added to the property list then I would like to be able to make available Feed.id

The abstract class looks like this:

Public MustInherit Class ItemClass
Inherits DynamicObject

Protected Shared propertyList As New Dictionary(Of String, String)
Protected Shared p_client As Phm.Adfero.Client
Protected Shared p_id As Integer
Protected Shared p_uri As Phm.Adfero.Uri
Protected Shared p_data As Phm.Adfero.Data
Protected Shared p_paramList As New Dictionary(Of String, String)

Sub New(ByRef client As Client, ByVal id As Integer)

    Dim instance As New Instance(Me)
    Dim instanceName = instance.getInstanceName()
    instance = Nothing

    If Not IsNothing(client) Then
        p_client = client
    Else
        Throw New ArgumentException("Object Parameter client is required!")
    End If

    If Not IsNothing(id) Then
        p_id = id
    Else
        Throw New ArgumentException("Integer Parameter id is required!")
    End If

    p_paramList.Add("identifier", instanceName & "s/" & p_id.ToString)

    p_uri = New Phm.Adfero.Uri(client, p_paramList)

    p_data = New Phm.Adfero.Data(client, p_uri)

End Sub


Protected Shared Sub transposeXML()

    Dim rootElement As String = p_data.Xml.DocumentElement.Name
    For Each baseElement As XmlNode In p_data.Xml.SelectSingleNode(rootElement)

        For Each element As XmlNode In baseElement
            If element.Name <> "fields" Then
                propertyList.Add(element.Name, element.InnerText)

            Else
                For Each field As XmlNode In baseElement.SelectSingleNode(element.Name).ChildNodes
                    For Each attrib As XmlAttribute In field.Attributes
                        propertyList.Add(attrib.Value, field.InnerText)
                    Next
                Next
            End If
        Next
    Next


End Sub

Public Overrides Function TryGetMember(ByVal binder As GetMemberBinder, _
                                       ByRef result As Object) As Boolean
    Dim name As String = binder.Name.ToLower()
    Return propertyList.TryGetValue(name, result)
End Function

Public Overrides Function TrySetMember(binder As SetMemberBinder, value As Object) As Boolean
    propertyList(binder.Name.ToLower()) = value
    Return True
End Function



End Class

An example of a derived class that inherits the above class is as follows:

Public Class Feed
Inherits ItemClass


Sub New(ByRef client As Client, ByRef id As Integer)
    MyBase.New(client, id)
End Sub

Public Overrides Function TryGetMember(binder As GetMemberBinder, ByRef result As Object) As Boolean
    Dim res As Object = Nothing
    Dim retVal As Boolean = MyBase.TryGetMember(binder, res)
    result = res
    Return retVal
End Function

Public Overrides Function TrySetMember(binder As System.Dynamic.SetMemberBinder, value As Object) As Boolean
    Return MyBase.TrySetMember(binder, value)
End Function

End Class

Thanks for any help in advance.

0

There are 0 answers