我想取一个类A的TYPE,然后再到另外的程序中以TYPE进行实例化,重新生成A,然后应用,谁能给一个例子啊?多谢!

解决方案 »

  1.   

    private object NewObject(Type AType, object AParent, string AText)
    {
        if (AType == null) return null;
        object Result = Activator.CreateInstance(AType);
        PropertyInfo vPropertyParent = AType.GetProperty("Parent");
        if (vPropertyParent != null)
            vPropertyParent.SetValue(Result, AParent, null);
        PropertyInfo vPropertyText = AType.GetProperty("Text");
        if (vPropertyText != null)
            vPropertyText.SetValue(Result, AText, null);
        return Result;
    }private void button1_Click(object sender, EventArgs e)
    {
        NewObject(button1.GetType(), this, "Zswang 路过");
    }
      

  2.   

    NewObject(typeof(Button), this, "Zswang 路过");这样也一样
      

  3.   

    To:zswang(伴水清清),多谢
    可我还是晕,还是看不明白。可能我说的不清楚,我是菜鸟级的,才学C#,在做一个插件式框架,插件里的Form1的内容需要在插件框架里显示并执行和Form1一样的功能操作,如果直接传递Form1,当框架里的Form关闭后无法重新生成,所以需要传递Form1的类型,然后在框架里生成,好象要用到反射机制,无法研究透,但是急需,麻烦帮忙给一个例程并注释一下,多谢!分不够再加。
      

  4.   

    Form vForm = (Form)NewObject(typeof(Form2), null, "Zswang 路过");
    vForm.Show();
      

  5.   

    简单来说就是通过Activator.CreateInstance(AType);创建类的实例
      

  6.   

    private object NewObject(Type AType, object AParent, string AText)
    AType是我要重新生成的类
    AParent是用来做什么的?
    AText是Form1.name吗?
      

  7.   

    Parent是容器,就是放置控件的地方AText就是控件的文字标题通过AType.GetProperty("Parent");得到属性信息
    这里是举例而已,你可以根据你的实际情况需要填写什么属性而作相应处理
      

  8.   

    多谢了,我去试试,大体明白了。给分。另外请去回复下下面两个贴,我觉得50分不足以表达我的感谢,另外看你是不是有兴趣?
    http://community.csdn.net/Expert/topic/5502/5502294.xml?temp=.7302515
    http://community.csdn.net/Expert/topic/5504/5504599.xml?temp=.8877527