反射实现:
string pt="~/pt/list.ascx";
UserControl uc = (UserControl)LoadControl(pt);
Type ct = uc.GetType();
PropertyInfo[] pInfo = ct.GetProperties();
foreach (PropertyInfo pi in pInfo)
{
if (pi.DeclaringType == ct)
{
string s = pi.Name;
object o = r[s];
if (o is string || o is int || o is bool)
{
ct.GetProperty(s).SetValue(uc, o, null);
}
}
}
Master.Master.FindControl("body").FindControl("m").Controls.Add(uc);
dynamic实现(失败):
string pt="~/pt/list.ascx";
dynamic uc = LoadControl(pt);
uc["n"] = 10;//这里无法知道属性名为"n"