C# aspx页面动态加载ascx用户控件 及 利用反射调用其内方法



//控件代码
public partial class WebUserControl : System.Web.UI.UserControl
{
    public void TestMethod(string strID)
    {
        this.TextBox1.Text += " WebUserControl:" + strID;
        //其他相关操作
    }
}

//控件代码
public partial class WebUserControl2 : System.Web.UI.UserControl
{
    public void TestMethod(string strID)
    {
        this.TextBox1.Text += " WebUserControl2:" + strID;
        //根据传入参数进行其他相关操作
    }
}

//页面代码
public partial class Default1 : System.Web.UI.Page
{
    bool isShow = true;//是
    string strWebUserControls = "WebUserControl,WebUserControl2";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (isShow)
        {
            string[] strUCs = strWebUserControls.Split(",".ToCharArray()[0]);
            for (int i = 0; i < strUCs.Length; i++)
            {
                string strUCName = strUCs[i].ToString();
                Control a = Page.LoadControl(strUCName+".ascx");
                a.ID = strUCName;
                this.Panel1.Controls.Add(a);
            }           
        } 
    }

    //页面按钮操作
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (isShow)
        {
            string[] strUCs = strWebUserControls.Split(",".ToCharArray()[0]);
            for (int i = 0; i < strUCs.Length; i++)
            {
                string strUCName = strUCs[i].ToString();
                Type tc = this.Panel1.FindControl(strUCName).GetType();
                Control uc = this.Panel1.FindControl(strUCName);
                object o = System.Activator.CreateInstance(uc.GetType());
                System.Reflection.MethodInfo m = tc.GetMethod("TestMethod");
                object[] objParas = new object[1];
                objParas[0] = "1";
                m.Invoke(uc, objParas);
                m.Invoke(a, null);
            }
        }
     }
}

=================================================================

C# 关于反射类[System.Reflection]的一个小例子[转载]
//新建一个类库项目,增加一个GetSum方法。
using System;

namespace ClassLibrary1
{
public class Class1
{
public Class1()
{
}
public int GetSum(int x, int y)
{
return x + y;
}
}
}

//再另建一个项目,在项目中引用上面生成的ClassLibrary1.DLL

System.Reflection.Assembly a = System.Reflection.Assembly.LoadFrom("ClassLibrary1.DLL");

System.Type t = a.GetType("ClassLibrary1.Class1");

//动态生成ClassLibrary1.Class类的实例
Object theObj = System.Activator.CreateInstance(t);

//参数信息,GetSum需要两个int参数
System.Type[] paramTypes = new System.Type[2];
paramTypes[0] = System.Type.GetType("System.Int32");
paramTypes[1] = System.Type.GetType("System.Int32");

//获取方法
System.Reflection.MethodInfo mi = t.GetMethod("GetSum", paramTypes);

//参数值
Object[] parameters = new Object[2];
parameters[0] = 3;
parameters[1] = 4;

//调用
Object returnValue = mi.Invoke(theObj, parameters);

Console.WriteLine("ClassLibrary1.Class1.GetSum(3, 4) returns: {0}", returnValue.ToString());


=================================================================


C#类的反射实例
http://www.cnblogs.com/hl8792/archive/2008/08/05/1261051.html
创建在一个命名空间下的类Class1和不在同一个命名空间下的Class1;
(在这里这些类只使用于被调用的,意义不大。反射用法在Form类里面)

Class1和Form 窗体在同一个命名空间
using System;
using System.Collections.Generic;
using System.Text;

namespace fanshetest1
{
class Class1
{
private string ab="1";
public Class1(string aa)
{
a = aa;
}
public int aa(int x,int y)
{
return x+y+x+y;
}
public string bb()
{
return "bb";
}
public static string cc()
{
return "cc";
}
public string AB
{
get
{
return ab;
}
set
{
ab = value;
}
}
public string a;
}
}

Class1和Form 窗体在不同一个命名空间
using System;
using System.Collections.Generic;
using System.Text;

namespace fanshetest1
{
class Class1
{
private string ab="1";
public Class1(string aa)
{
a = aa;
}
public int aa(int x,int y)
{
return x+y+x+y;
}
public string bb()
{
return "bb";
}
public static string cc()
{
return "cc";
}
public string AB
{
get
{
return ab;
}
set
{
ab = value;
}
}
public string a;
}
}
下面是如何使用反射操作以上类;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace fanshetest
{
public partial class Form1 : Form
{
System.Reflection.Assembly a;
System.Reflection.Assembly b;
public Form1()
{
InitializeComponent();
a = System.Reflection.Assembly.LoadFrom("Class1.DLL");
}

private void button1_Click(object sender, EventArgs e)
{
gouzaohanshu();


}
//没有传参数,返回一个类型;
private void One()
{
//再另建一个项目,在项目中引用上面生成的ClassLibrary1.DLL
System.Type t = a.GetType("Class1.Class1");
//动态生成ClassLibrary1.Class类的实例
Object theObj = System.Activator.CreateInstance(t);
//参数信息,GetSum需要两个string参数
System.Type[] paramTypes = new System.Type[2];
paramTypes[0] = System.Type.GetType("System.String");
paramTypes[1] = System.Type.GetType("System.String");
//System.Reflection.MethodInfo mi = t.GetMethod("aa", paramTypes);
System.Reflection.MethodInfo mi = t.GetMethod("bb");
//参数值
Object[] parameters = new Object[2];
parameters[0] = 3;
parameters[1] = 4;

Object returnValue = mi.Invoke(theObj, null);
this.textBox1.Text = returnValue.ToString();
}
//没有返回值,调用的是直接执行函数
private void None_void()
{
//再另建一个项目,在项目中引用上面生成的ClassLibrary1.DLL
System.Type t = a.GetType("Class1.Class2");
//动态生成ClassLibrary1.Class类的实例
Object theObj = System.Activator.CreateInstance(t);
//参数信息,GetSum需要两个string参数
System.Type[] paramTypes = new System.Type[2];
//此处调用方法,如果有参数只需要在括号的后面加上"," 加上参数的type[]类型的参数
System.Reflection.MethodInfo mi = t.GetMethod("Mes");
Object returnValue = mi.Invoke(theObj, null);

}
//没有返回值,传出参数接收返回值;
private void Two()
{
Type t = a.GetType("Class1.Class1");
Object theObj = Activator.CreateInstance(t);
Type[] types=new Type[2];
types[0]=Type.GetType("System.Int32");
types[1]=Type.GetType("System.Int32");
Object[] obj=new Object[2];
obj[0]=2;
obj[1]=3;
MethodInfo mi = t.GetMethod("aa",types);
Object returnValue=mi.Invoke(theObj,obj);
this.textBox1.Text = returnValue.ToString();
}
//获取同一个命名空间的反射出值
private void Local()
{
Type t=Type.GetType("fanshetest1.Class1");
//创建一个 实例
Object theObj = Activator.CreateInstance(t);
Type[] types=new Type[2];
types[0]=Type.GetType("System.Int32");
types[1] = Type.GetType("System.Int32");
Object[] obj = new Object[2];
obj[0] = 2;
obj[1] = 3;
MethodInfo mi = t.GetMethod("aa",types);
Object returnValue = mi.Invoke(theObj,obj);
this.textBox1.Text = returnValue.ToString();
}
//获取出一个属性
private void attribute()
{
a = Assembly.LoadFrom("Class1.dll");
Type t = a.GetType("Class1.Class1");
t = Type.GetType("fanshetest1.Class1");
Object theObj = Activator.CreateInstance(t);
t.GetProperty("AB").SetValue(theObj,"a",null);
this.textBox1.Text = t.GetProperty("AB").GetValue(theObj, null).ToString();
}
//获取出静态的函数;
private void static_()
{
a = System.Reflection.Assembly.LoadFrom("Class1.dll");
Type t = a.GetType("Class1.Class1");
t = Type.GetType("fanshetest1.Class1");
//创建一个实例
Object theObj = Activator.CreateInstance(t);
//创建一个方法的实例
MethodInfo mi = t.GetMethod("cc");
Object returnValue = mi.Invoke(theObj, null);
this.textBox1.Text = returnValue.ToString();
}
//获取出变量;
private void variable()
{
a = Assembly.LoadFrom("Class1.dll");
Type t = a.GetType("Class1.Class1");
t = Type.GetType("fanshetest1.Class1");
Object theObj = Activator.CreateInstance(t);
MethodInfo mi = t.GetMethod("a");
t.GetField("a").SetValue(theObj,"a");
this.textBox1.Text = t.GetField("a").GetValue(theObj).ToString();
}
//获取出私有变量反射值;
private void private_()
{

}
//构造函数
private void gouzaohanshu()
{
Type t = a.GetType("Class1.Class1");
t = Type.GetType("fanshetest1.Class1");
//创建构造函数类型
Type[] structure_Type = new Type[1];
structure_Type[0] = Type.GetType("System.String");
//定义构造函数传参类型
Object[] structure_Obj = new Object[1];
structure_Obj[0] = "aa";
//创建一个 实例
Object theObj = Activator.CreateInstance(t,structure_Obj);
//MethodInfo structure_Mi = t.GetConstructor(structure_Type);
//MethodInfo structure_Mi = t.GetMethod("Class1", structure_Type);
//structure_Mi.Invoke(theObj, structure_Obj);

MethodInfo mi = t.GetMethod("a");
//t.GetField("a").SetValue(theObj, "a");
this.textBox1.Text = t.GetField("a").GetValue(theObj).ToString();
}

}
}


=================================================================

Type 是有scope的,
要取得System.Web.* 的Type Instance,
必須先取得System.Web 的Assembly,
書中是以FindType函式來掃描目前AppDomain中的所有Assembly
並找到傳入字串的Type,該函式列表如下:


private Type FindType(string typeName)
{
foreach(Assembly assem in AppDomain.CurrentDomain.GetAssemblies())
{
Type t = assem.GetType(typeName);
if(t != null)
return t;
}
return null;
}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页