public T ClassConvert<T>(object o)
where T:new()
{
T t = new T();
Type type = o.GetType();
System.Reflection.PropertyInfo[] ps = type.GetProperties();
foreach (PropertyInfo p in ps)
{
object pv = p.GetValue(o,null);
Type type2 = t.GetType();
PropertyInfo pr = type2.GetProperty(p.Name);
if(pr!=null)
{
pr.SetValue(t, pv, null);
}
}
return t;
}
对于复杂类,可以先序列后,改个类名,再反序列化来进行maping
cy b = new cy();
b.currency_ID = "33";
b.currency_name = "44";
b.currency_name_en = "55";
b.rrrr = "ttt";
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlSerializer xs = new XmlSerializer(typeof(cy));
xs.Serialize(sw, b);
sw.Close();
string aaa = sb.ToString();
aaa=aaa.Replace("<cy ","<cc ").Replace("</cy>","</cc>");
xs = new XmlSerializer(typeof(cc));
StringReader sr = new StringReader(aaa);
cc ddd = (cc)xs.Deserialize(sr);