遍历一个实体对象,该实体对象是一个树形,并且获取所有的值类型,若是引用类型继续向下递归,如何实现?
internal void Test(Type type, string _xPath, XmlNamespaceManager _xmlNamespaceManager, XmlDocument _xmlDocument, object instance)
{ System.Reflection.PropertyInfo[] _ropertyInfos = type.GetProperties();
foreach (System.Reflection.PropertyInfo _propertyInfo in _ropertyInfos)
{
//Find all the attributes
object[] _objects = _propertyInfo.GetCustomAttributes(false);
//Assume the current property has not the XmlElementAttribute attribute.
bool isHasNoXmlAttribute = false;
foreach (object o in _objects)
{
XmlAttributeAttribute _xmlAttributeAttribute = o as XmlAttributeAttribute;
if (_xmlAttributeAttribute != null)
{
isHasNoXmlAttribute = true; if (_propertyInfo.PropertyType == typeof(String))
{
string propertyValue = _propertyInfo.GetValue(关键是这里,无法获取孩子孙子对象的实体, null).ToString();
XmlNode _xmlNode = _xmlDocument.DocumentElement.SelectSingleNode(_xPath, _xmlNamespaceManager);
//Assert.AreEqual(propertyValue, _xmlNode.Attributes[_xmlAttributeAttribute.a].Value);
}
}
XmlElementAttribute _xmlElementAttribute = o as XmlElementAttribute;
if (_xmlElementAttribute != null)
{
//the XmlElementAttribute attribute is existed.
isHasNoXmlAttribute = true;
string xmlns = this.GetXPath(_xmlElementAttribute.Namespace);
string typeName = _xmlElementAttribute.ElementName;
_xPath += "/" + xmlns + ":" + typeName;
instance = 这里很难实现.
this.Test(_propertyInfo.PropertyType, _xPath, _xmlNamespaceManager, _xmlDocument, instance);
}
}
//In the end, not find the XmlElementAttribute attribute.
if (!isHasNoXmlAttribute)
{
Assert.Fail("ASDFASDFASDFASDFASDFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
}
}
}
谢谢!
internal void Test(Type type, string _xPath, XmlNamespaceManager _xmlNamespaceManager, XmlDocument _xmlDocument, object instance)
{ System.Reflection.PropertyInfo[] _ropertyInfos = type.GetProperties();
foreach (System.Reflection.PropertyInfo _propertyInfo in _ropertyInfos)
{
//Find all the attributes
object[] _objects = _propertyInfo.GetCustomAttributes(false);
//Assume the current property has not the XmlElementAttribute attribute.
bool isHasNoXmlAttribute = false;
foreach (object o in _objects)
{
XmlAttributeAttribute _xmlAttributeAttribute = o as XmlAttributeAttribute;
if (_xmlAttributeAttribute != null)
{
isHasNoXmlAttribute = true; if (_propertyInfo.PropertyType == typeof(String))
{
string propertyValue = _propertyInfo.GetValue(关键是这里,无法获取孩子孙子对象的实体, null).ToString();
XmlNode _xmlNode = _xmlDocument.DocumentElement.SelectSingleNode(_xPath, _xmlNamespaceManager);
//Assert.AreEqual(propertyValue, _xmlNode.Attributes[_xmlAttributeAttribute.a].Value);
}
}
XmlElementAttribute _xmlElementAttribute = o as XmlElementAttribute;
if (_xmlElementAttribute != null)
{
//the XmlElementAttribute attribute is existed.
isHasNoXmlAttribute = true;
string xmlns = this.GetXPath(_xmlElementAttribute.Namespace);
string typeName = _xmlElementAttribute.ElementName;
_xPath += "/" + xmlns + ":" + typeName;
instance = 这里很难实现.
this.Test(_propertyInfo.PropertyType, _xPath, _xmlNamespaceManager, _xmlDocument, instance);
}
}
//In the end, not find the XmlElementAttribute attribute.
if (!isHasNoXmlAttribute)
{
Assert.Fail("ASDFASDFASDFASDFASDFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
}
}
}
谢谢!
所以它本身是一个树形结构
关键是递归的时候
string propertyValue = _propertyInfo.GetValue(关键是这里,无法获取孩子孙子对象的实体, null).ToString();
获取属性的值的时候,他的实体对象(该属性所在的类)不知道该怎么获取?