有关转换问题
Convert.ChangeType 的運用
JOBEntity job=new JOBEntity();
Type t = job.GetType();
PropertyInfo[] info = t.GetProperties();
?foreach(PropertyInfo i in info)
{
? ? ? ?//value 必須保證有效性,否則要對 PropertyType 進行 typeof?判斷?
? ? ? ?if(i.PropertyType.IsGenericType)//是否為泛形類型
? ? ? ? ? ? i.SetValue(job, Convert.ChangeType(value, i.PropertyType.GetGenericArguments()[0]), null);
? ? ? ?else
? ? ? ? ? ? i.SetValue(job, Convert.ChangeType(value,i.PropertyType), null);
}
-----------------------------------------------------------------------------
實體轉換成XML
[Serializable]
public class SmtpConfig
{
public string Host{get;set;}
public int Port{get;set;}
}
SmtpConfig?_config=new?SmtpConfig{Host="127.0.0.1", Port=21;}
using (StreamWriter sw = new StreamWriter(xmlpath, false, System.Text.Encoding.Default))
{
XmlSerializer xs = new XmlSerializer(typeof(SmtpConfig));
xs.Serialize(sw, _config);
sw.Close();
}
?
-------------------------------------------------
IDataRecord 轉換為實體對象
private static void ReaderToEntity(IDataRecord reader, Object entity) { for (int i = 0; i < reader.FieldCount; i++) { System.Reflection.PropertyInfo propertyInfo = entity.GetType().GetProperty(reader.GetName(i)); if (propertyInfo != null) { if (reader.GetValue(i) != DBNull.Value) { propertyInfo.SetValue(entity, reader.GetValue(i), null); } } } }?
轉載于:https://www.cnblogs.com/yipeng-yu/archive/2012/03/07/2383645.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
- 上一篇: iTextSharp应用,生成pdf
- 下一篇: SQL Azure Reporting