public class Test { public string stu; pulic int age; public classFirst cfirst; pulbic classSecond csecond; } public class classFirst { //todo } public class classSecond { //todo } 我想遍历Test。 但是遇到cfirst他时,怎么让程序转到classFirst中处理。 public classFirst cfirst; 要求不创建实例。
修改ClassSerialize的参数类型,Object改成Type using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Reflection; using System.Collections;namespace ConsoleApplication1 { class O { public int a; public string b; public SO c;
} class SO { public int a; public int b;
} class Program { static void Main(string[] args) { XmlDocument xmlDoc= new XmlDocument(); var a=ClassSerialize(xmlDoc,typeof(O)); } public static XmlElement ClassSerialize(XmlDocument xmlDoc, Type obj) { GC.Collect(); XmlElement elementRoot = null; XmlElement elementTwo = null; XmlElement elementOne = null; //Type typeobj = obj.GetType(); Type typeobj = obj; FieldInfo[] fieldInfo = typeobj.GetFields(); PropertyInfo[] propertyInfo = typeobj.GetProperties(); elementTwo = xmlDoc.CreateElement(typeobj.Name); for (int i = 0; i < fieldInfo.Length; i++) { elementOne = xmlDoc.CreateElement(typeobj.Name); foreach (FieldInfo _fieldInfo in fieldInfo) { //判断字段是否是集合 if (typeof(ICollection).IsAssignableFrom(_fieldInfo.FieldType)) { Console.WriteLine("我是一个集合,有待处理"); } //判断字段是否是类 else if (_fieldInfo.FieldType.IsClass) { //Type ty = _fieldInfo.FieldType; if (_fieldInfo.FieldType != typeof(String)) { //object asm = Activator.CreateInstance(_fieldInfo.FieldType); //我的意思是用另一种方法代替红色所标的内容。 // elementRoot = ClassSerialize(xmlDoc, asm); elementRoot = ClassSerialize(xmlDoc, _fieldInfo.FieldType); elementOne.AppendChild(elementRoot); //break; } else { Console.WriteLine("我是一个String"); elementOne.SetAttribute(_fieldInfo.Name, null); elementTwo.AppendChild(elementOne); } } //该字段是值类型的 else { elementOne.SetAttribute(_fieldInfo.Name, _fieldInfo.GetValue(Activator.CreateInstance(typeobj)).ToString()); elementTwo.AppendChild(elementOne); } } break; } return elementTwo; } } }
不创建就直接用classFirst找呗。
{
public string stu;
pulic int age;
public classFirst cfirst;
pulbic classSecond csecond;
}
public class classFirst
{
//todo
}
public class classSecond
{
//todo
}
我想遍历Test。
但是遇到cfirst他时,怎么让程序转到classFirst中处理。
public classFirst cfirst;
要求不创建实例。
{
GC.Collect();
XmlElement elementRoot = null;
XmlElement elementTwo = null;
XmlElement elementOne = null;
Type typeobj = obj.GetType();
FieldInfo[] fieldInfo = typeobj.GetFields();
PropertyInfo[] propertyInfo = typeobj.GetProperties();
elementTwo = xmlDoc.CreateElement(typeobj.Name); for (int i = 0; i < fieldInfo.Length; i++)
{
elementOne = xmlDoc.CreateElement(typeobj.Name);
foreach (FieldInfo _fieldInfo in fieldInfo)
{
//判断字段是否是集合
if (typeof(ICollection).IsAssignableFrom(_fieldInfo.FieldType))
{
Console.WriteLine("我是一个集合,有待处理");
}
//判断字段是否是类
else if (_fieldInfo.FieldType.IsClass)
{
//Type ty = _fieldInfo.FieldType;
if (_fieldInfo.FieldType != typeof(String))
{
object asm = Activator.CreateInstance(_fieldInfo.FieldType);
我的意思是用另一种方法代替红色所标的内容。
elementRoot = ClassSerialize(xmlDoc, _object);
elementOne.AppendChild(elementRoot);
//break;
}
else
{
Console.WriteLine("我是一个String");
elementOne.SetAttribute(_fieldInfo.Name, null);
elementTwo.AppendChild(elementOne);
}
}
//该字段是值类型的
else
{
elementOne.SetAttribute(_fieldInfo.Name, _fieldInfo.GetValue(Activator.CreateInstance(typeobj)).ToString());
elementTwo.AppendChild(elementOne);
}
}
break;
}
return elementTwo;
}
{
GC.Collect();
XmlElement elementRoot = null;
XmlElement elementTwo = null;
XmlElement elementOne = null;
Type typeobj = obj.GetType();
FieldInfo[] fieldInfo = typeobj.GetFields();
PropertyInfo[] propertyInfo = typeobj.GetProperties();
elementTwo = xmlDoc.CreateElement(typeobj.Name); for (int i = 0; i < fieldInfo.Length; i++)
{
elementOne = xmlDoc.CreateElement(typeobj.Name);
foreach (FieldInfo _fieldInfo in fieldInfo)
{
//判断字段是否是集合
if (typeof(ICollection).IsAssignableFrom(_fieldInfo.FieldType))
{
Console.WriteLine("我是一个集合,有待处理");
}
//判断字段是否是类
else if (_fieldInfo.FieldType.IsClass)
{
//Type ty = _fieldInfo.FieldType;
if (_fieldInfo.FieldType != typeof(String))
{
object asm = Activator.CreateInstance(_fieldInfo.FieldType);
我的意思是用另一种方法代替红色所标的内容。
elementRoot = ClassSerialize(xmlDoc, asm);
elementOne.AppendChild(elementRoot);
//break;
}
else
{
Console.WriteLine("我是一个String");
elementOne.SetAttribute(_fieldInfo.Name, null);
elementTwo.AppendChild(elementOne);
}
}
//该字段是值类型的
else
{
elementOne.SetAttribute(_fieldInfo.Name, _fieldInfo.GetValue(Activator.CreateInstance(typeobj)).ToString());
elementTwo.AppendChild(elementOne);
}
}
break;
}
return elementTwo;
}
把6楼中的_object 改为asm 就可以了。那段代码就没有错了。就是想通过其他的方法来代替 6楼中红色所标的内容的功能。
期待..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Collections;namespace ConsoleApplication1
{ class O
{
public int a; public string b; public SO c;
} class SO
{
public int a;
public int b;
} class Program
{
static void Main(string[] args)
{ XmlDocument xmlDoc= new XmlDocument(); var a=ClassSerialize(xmlDoc,typeof(O)); } public static XmlElement ClassSerialize(XmlDocument xmlDoc, Type obj)
{
GC.Collect();
XmlElement elementRoot = null;
XmlElement elementTwo = null;
XmlElement elementOne = null;
//Type typeobj = obj.GetType();
Type typeobj = obj; FieldInfo[] fieldInfo = typeobj.GetFields();
PropertyInfo[] propertyInfo = typeobj.GetProperties();
elementTwo = xmlDoc.CreateElement(typeobj.Name); for (int i = 0; i < fieldInfo.Length; i++)
{
elementOne = xmlDoc.CreateElement(typeobj.Name);
foreach (FieldInfo _fieldInfo in fieldInfo)
{
//判断字段是否是集合
if (typeof(ICollection).IsAssignableFrom(_fieldInfo.FieldType))
{
Console.WriteLine("我是一个集合,有待处理");
}
//判断字段是否是类
else if (_fieldInfo.FieldType.IsClass)
{
//Type ty = _fieldInfo.FieldType;
if (_fieldInfo.FieldType != typeof(String))
{
//object asm = Activator.CreateInstance(_fieldInfo.FieldType);
//我的意思是用另一种方法代替红色所标的内容。
// elementRoot = ClassSerialize(xmlDoc, asm);
elementRoot = ClassSerialize(xmlDoc, _fieldInfo.FieldType); elementOne.AppendChild(elementRoot);
//break;
}
else
{
Console.WriteLine("我是一个String");
elementOne.SetAttribute(_fieldInfo.Name, null);
elementTwo.AppendChild(elementOne);
}
}
//该字段是值类型的
else
{
elementOne.SetAttribute(_fieldInfo.Name, _fieldInfo.GetValue(Activator.CreateInstance(typeobj)).ToString());
elementTwo.AppendChild(elementOne);
}
}
break;
}
return elementTwo;
}
}
}
就是针对String类型的。
在测试的对象中如有这样的public String str;
若str没有初始值;
在运行上述代码会出错。
我是这样取值滴...
else
{
Console.WriteLine("我是一个String");
elementOne.SetAttribute(_fieldInfo.Name, _fieldInfo.GetValue(Activator.CreateInstance(typeobj)).ToString());
);
elementTwo.AppendChild(elementOne);
}