Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 为什么在同一个扩展方法中不使用扩展方法?_Vb.net_Reflection_Extension Methods_Dynamic Programming_Generic Programming - Fatal编程技术网

Vb.net 为什么在同一个扩展方法中不使用扩展方法?

Vb.net 为什么在同一个扩展方法中不使用扩展方法?,vb.net,reflection,extension-methods,dynamic-programming,generic-programming,Vb.net,Reflection,Extension Methods,Dynamic Programming,Generic Programming,我得到了一个扩展方法,它为我提供了实例中每个属性的值。对于标量值,它工作得很好。但是对于集合有一个问题。这是我的代码: <Extension()> Public Function ToXml(Of T)(ByVal source As T) As XmlDocument Dim oXmlDocument As New XmlDocument oXmlDocument.AppendChild(oXmlDocument.CreateXmlDeclaration("1.0"

我得到了一个扩展方法,它为我提供了实例中每个属性的值。对于标量值,它工作得很好。但是对于
集合
有一个问题。这是我的代码:

<Extension()>
Public Function ToXml(Of T)(ByVal source As T) As XmlDocument
    Dim oXmlDocument As New XmlDocument
    oXmlDocument.AppendChild(oXmlDocument.CreateXmlDeclaration("1.0", "utf-8", Nothing))
    oXmlDocument.AppendChild(oXmlDocument.CreateElement(XmlConvert.EncodeName(source.GetType.ToString)))

    For Each Item As System.Reflection.FieldInfo In source.GetType.GetFields
        Dim oElement As XmlElement = oXmlDocument.CreateElement(XmlConvert.EncodeName(Item.MemberType.ToString))
        oElement.Attributes.Append(oXmlDocument.CreateAttribute("Name")).Value = Item.Name
        oElement.Attributes.Append(oXmlDocument.CreateAttribute("Value")).Value = Item.GetValue(source)

        oXmlDocument.DocumentElement.AppendChild(oElement)
    Next

    For Each Item As System.Reflection.PropertyInfo In source.GetType.GetProperties
        Dim oElement As XmlElement = oXmlDocument.CreateElement(XmlConvert.EncodeName(Item.MemberType.ToString))
        oElement.Attributes.Append(oXmlDocument.CreateAttribute("Name")).Value = Item.Name

        If (Not (TryCast(Item.GetValue(source, Nothing), ICollection) Is Nothing)) Then
            For Each SubItem As Object In CType(Item.GetValue(source, Nothing), ICollection)
                For Each Node As XmlNode In SubItem.ToXml().DocumentElement.SelectNodes("node()")
                    oElement.AppendChild(oElement.OwnerDocument.ImportNode(Node, True))
                Next
            Next
        Else
            oElement.Attributes.Append(oXmlDocument.CreateAttribute("Value")).Value = If(Not (Item.GetValue(source, Nothing) Is Nothing), Item.GetValue(source, Nothing).ToString, "Nothing")
        End If

        oXmlDocument.DocumentElement.AppendChild(oElement)
    Next

    Return oXmlDocument
End Function
未找到“MyClass”类型的公共成员“ToXml”引发错误。

但如果我这样做了

Dim instance As new MyClass
instance.ToXml()
这也行得通。我的错在哪里


提前感谢您的回复。

在VB.NET中,为了向后兼容,扩展方法对声明为
对象的变量不起作用

尝试:

它将失败

因此,您不能对
子项
调用
ToXml
,因为
子项
的类型为
对象


但是,您可以像常规方法一样调用
ToXml

For Each Node As XmlNode In ToXml(SubItem).DocumentElement.SelectNodes("node()")
Dim instance As Object = new MyClass()
instance.ToXml()
For Each Node As XmlNode In ToXml(SubItem).DocumentElement.SelectNodes("node()")