/// <summary>
/// 方法目的:当一个类(T)已经有值,但是这个时候需要对另一个类(Tn)赋值
/// 并且 两个类存在相同字段名称(最终目的把两个类中相同字段直接赋值);
/// 例:class MenuTB{ public string Name{get;set;}} class ShowMenuModel{ public string Name{get;set;}}
/// 备注:下面例子Tn是继承T后新的类返回值
/// T->MenuTB
/// Tn->ShowMenuModel
// 调用方法:ShowMenuModel fmodel = ModelToModel<MenuTB, ShowMenuModel>(frow,new ShowMenuModel());
/// </summary>
/// <typeparam name="T">被继承的类</typeparam>
/// <typeparam name="Tn">继承T后新的类</typeparam>
/// <param name="objmodel">T类的参数值</param>
/// <param name="nobjModel">Tn类一个空的实例化</param>
/// <returns></returns>
public Tn ModelToModel<T, Tn>(T objmodel, Tn nobjModel)
{
Tn Nobjmodel = nobjModel;
//被继承的类
Type modelType = typeof(T);
Type newModel = typeof(Tn);//继承后的新类
PropertyInfo[] newpropertys = modelType.GetProperties();//列举出新的类中信息
//循环新类的属性
foreach (PropertyInfo pi in newpropertys)
{
string ParaName = pi.Name;//参数名称
string ParamType = pi.PropertyType.Name;//字段类型 int/DateTime/string 等等
object vaule = modelType.GetProperty(ParaName).GetValue(objmodel, null);//从被继承的类中根据相同字段名称读取类的值
newModel.GetProperty(ParaName).SetValue(Nobjmodel, vaule, null);//给新版本的类参数赋值
}
return Nobjmodel;
}