0

我不能让它工作!我希望能够在继承自 system.dynamic.dynamicobject 的基类的派生类上创建动态属性?

我创建了一个 VB.net 抽象类,我想从该抽象类继承的实例上使用动态属性继承该抽象类。基类读取 XML 文档并将元素和任何属性转换为属性列表 Dictionary。

这个想法是,当派生类被实例化时,抽象类 propertyList 中列出的所有项目都作为公共属性公开。从我在下面列出的代码中,如果将属性 id 添加到属性列表中,那么我希望能够使 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

感谢您提前提供任何帮助。

4

0 回答 0