我要的功能是
动态加载一个用户控件,对这个用户控件里面的一个属性进行赋值(这个属性是一个类对像)
----------
以下是源码
用户控件:Father.ascxusing System;/// <summary>
/// 一个父亲用户控件
/// </summary>
public partial class Father : System.Web.UI.UserControl
{    Son _Children;
    /// <summary>
    /// 声明父亲用户控件下面有一个孩子的属性
    /// </summary>
    public Son Children
    {
        get { return _Children; }
        set
        {
            OutValue(value);
        }
    }    public void OutValue(Son son)
    {
        Response.Write(son.Name);
    }
}/// <summary>
/// 起明儿子类
/// </summary>
public class Son
{
    public string Name { get; set; }
    public int Age { get; set; }
}
动态添加的页面Default5.aspxusing System;
using System.Web.UI;
using System.Reflection;
public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //加载一个父亲用户控件
        Control userControl = LoadControl("Father.ascx");
        //取出这个Control里面的Son对像
        System.Reflection.PropertyInfo pi = userControl.GetType().GetProperty("Children");
        if (pi != null)
        {
            XX = ??
            //这里如何声明一个pi的对像           
            //取得儿子对像的名称属性
            System.Reflection.PropertyInfo p = pi.GetType().GetProperty("Name");
            if (p != null)
            {
                p.SetValue(XX, "草泥马", null);    
                //这里我要如何正确的将 "草呢马" 赋值给 上面那个不知道如何创建的对像
                //谢谢
            }
        }
    }
}

解决方案 »

  1.   


                Control userControl = LoadControl("Father.ascx");
                System.Reflection.PropertyInfo pi = userControl.GetType().GetProperty("Children");
                if (pi != null)
                {
                    object obj = Activator.CreateInstance(pi.PropertyType);
                    System.Reflection.PropertyInfo pName = pi.PropertyType.GetProperty("Name");
                    pName.SetValue(obj,"ojlovecd", null);
                    System.Reflection.PropertyInfo pAge = pi.PropertyType.GetProperty("Age");
                    pAge.SetValue(obj, 26, null);
                    pi.SetValue(userControl, obj, null);
                }
      

  2.   

    //加载一个父亲用户控件
                System.Web.UI.Control userControl = LoadControl("~/Control/Father.ascx");
                //取出这个Control里面的Son对像
                System.Reflection.PropertyInfo pi = userControl.GetType().GetProperty("Children");
                if (pi != null)
                {
                    object obj = System.Activator.CreateInstance(pi.PropertyType);
                    //这里如何声明一个pi的对像
                    System.Reflection.PropertyInfo p = pi.GetType().GetProperty("Name");
                    if (p != null)
                    {
                        p.SetValue(obj, "草呢妈真贱啊你,草!", null);
                    }
                    pi.SetValue(userControl, obj, null);
                }