出错提示:
类型“NekoNH.Data.RootSort”的对象无法转换为类型“NekoNH.Data.RootSort”。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.ArgumentException: 类型“NekoNH.Data.RootSort”的对象无法转换为类型“NekoNH.Data.RootSort”。
源错误:
行 108: Response.Write("<br>");
行 109:
行 110: pInfo.SetValue(obj, value, null);
行 111:
行 112:
源代码:
protected void NekoDetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
RootSort trs = (RootSort)BLLRootSort.SelectRecord(rootSort);
RootSort rs = new RootSort();
rs.CopyFromWithId(trs);
string NHpath = string.Format("{0}", Server.MapPath("~/bin/NekoNH.Data.dll"));
string NHname = string.Format("NekoNH.Data.{0}",rs.ClassName);
//装载类
Assembly assembly = Assembly.LoadFile(NHpath);
//获得类型
Type NHt = assembly.GetType(NHname);
//实例化一个新类
Object obj = Activator.CreateInstance(NHt);
//给实例化类的属性设置值
//类别名称
SetPropertyInfo(obj, "Name",((TextBox)NekoDetailsView1.FindControl("TextBox_Name")).Text);
//类名称
SetPropertyInfo(obj, "ClassName", rs.ClassName);
//顺序
SetPropertyInfo(obj, "Sequence",Convert.ToInt32(((TextBox)NekoDetailsView1.FindControl("TextBox_Sequence")).Text));
//是否启用
SetPropertyInfo(obj, "IsPublic", ((CheckBox)NekoDetailsView1.FindControl("CheckBox_IsPublic")).Checked);
//备注
SetPropertyInfo(obj, "Remark", ((TextBox)NekoDetailsView1.FindControl("TextBox_Remark")).Text);
//父类别
SetPropertyInfo(obj, "ParentSort", rs);
//SetPropertyInfo(obj, "ParentSort", BLLRootSort.SelectRecord(Convert.ToInt16(TreeSort1.Value)));
//Response.Write(pInfo.GetValue(obj, null));
//e.Values["ParentSort"] = BLLRootSort.SelectRecord(0);
//WebMethod.ShowMessage("添加数据成功。");
e.Cancel = true;
}
/// <summary>
/// 设置对象属性的值
/// </summary>
/// <param name="obj">对象</param>
/// <param name="name">属性名称</param>
/// <param name="value">object值</param>
protected void SetPropertyInfo(object obj, string name,object value)
{
Type t = obj.GetType();
PropertyInfo pInfo = t.GetProperty(name);
Response.Write("pInfo " + pInfo.GetType());
Response.Write("<br>");
Response.Write("pInfo " + t.GetType());
Response.Write("<br>");
Response.Write("value " + value.GetType());
Response.Write("<br>");
Response.Write("<br>");
pInfo.SetValue(obj, value, null);
}
类的关系:
RootSort类
private int _id;
/// <summary>
/// 类别名称
/// </summary>
private string _name;
/// <summary>
/// 父类名称
/// </summary>
private string _className;
/// <summary>
/// 顺序
/// </summary>
private int _sequence;
/// <summary>
/// 是否启用
/// </summary>
private bool _isPublic;
/// <summary>
/// 备注
/// </summary>
private string _remark;
/// <summary>
///
/// </summary>
private RootSort _parentSort;
/// <summary>
///
/// </summary>
private IList<RootSort> _parentSortRootSorts;
NewsSort类继承RootSort类
CallingSort类继承RootSort类