1
인스턴스의 모든 속성 값을 제공하는 확장 메서드가 있습니다. 스칼라 값은 괜찮습니다. 그러나 Collections
의 경우 문제가 있습니다.동일한 확장 메서드에서 확장 메서드를 사용하면 왜 작동하지 않습니까?
<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
이
라인
For Each Node As XmlNode In SubItem.ToXml().DocumentElement.SelectNodes("node()")
오류 Public member 'ToXml' on type 'MyClass' not found.
하지만 내가 할 경우
Dim instance As new MyClass
instance.ToXml()
에게 던졌습니다 : 이것은 내 코드입니다
이것도 작동합니다. 내 잘못은 어디에 있습니까?
미리 답변 해 주셔서 감사합니다.
감사합니다. 정상적인 방법으로 호출하는 방법 :-) – mburm