Динамические свойства Vb.net на унаследованных объектах

Я не могу заставить это работать! Я хочу иметь возможность создавать динамические свойства для производных классов, которые наследуются от базового класса, который наследуется от system.dynamic.dynamicobject?

Я создал абстрактный класс VB.net, от которого я хочу наследовать с динамическими свойствами в экземплярах, которые наследуются от этого абстрактного класса. Базовый класс читает документ XML и переносит элементы и любые атрибуты в словарь списка свойств.

Идея состоит в том, что при создании экземпляра производного класса все элементы, перечисленные в абстрактном классе propertyList, представляются как открытые свойства. Из кода, который я перечислил ниже, если идентификатор свойства добавлен в список свойств, я хотел бы иметь возможность сделать доступным Feed.id

Абстрактный класс выглядит так:

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

Пример производного класса, который наследует вышеуказанный класс, выглядит следующим образом:

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

Спасибо за любую помощь заранее.

0 ответов

Другие вопросы по тегам