2 回答

TA貢獻1797條經(jīng)驗 獲得超6個贊
用反射:
主調(diào)類:
private void button3_Click(object sender, EventArgs e)
{
bindForm("WindowsApplication1.UserControl1");
}
private void bindForm(string f)
{
System.Type t = System.Type.GetType(f);
object ff = Activator.CreateInstance(t,null);
System.Reflection.MethodInfo method = t.GetMethod("setText");
System.Reflection. BindingFlags flag = System.Reflection. BindingFlags.Public |System.Reflection. BindingFlags.Instance;
object returnValue = method.Invoke(ff, null);
}
各窗體:
public partial class UserControl1 : Form
{
public UserControl1()
{
InitializeComponent();
}
public void setText()
{
Text = "kkk";
Show();
}
}

TA貢獻1946條經(jīng)驗 獲得超3個贊
public void rename(類型? ci,string stri) //???這個地方怎么定義參數(shù)ci???
用object ci
對象.萬類之根~~
剩下的不說啦哈哈~
最好你用
public void rename(ref string name,string stri)用引用參數(shù)
{
name = stri;
}
rename(class1.name,"123");
//然后這里class1.name 就被設(shè)成"123"了
反射機制-----本人以前也只稍微研究過一會,也不太熟悉,以下提供為參考
System.Reflection.Assembly
Assembly assembly = Assembly.LoadFile("dll絕對路徑");
Module[] modules = assembly.GetModules();
//得到方法
---------------------------------------------
//獲取類型信息
Type t = Type.GetType("TestSpace.TestClass");
//構(gòu)造器的參數(shù)
object[] constuctParms = new object[]{"timmy"};
//根據(jù)類型創(chuàng)建對象
object dObj = Activator.CreateInstance(t,constuctParms);
//獲取方法的信息
MethodInfo method = t.GetMethod("GetValue");
//調(diào)用方法的一些標(biāo)志位,這里的含義是Public并且是實例方法,這也是默認(rèn)的值
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
//GetValue方法的參數(shù)
object[] parameters = new object[]{"Hello"};
//調(diào)用方法,用一個object接收返回值
object returnValue = method.Invoke(dObj,flag,Type.DefaultBinder,parameters,null);
個人見解,反射用起來巨耗性能,可以稱為是華麗的招數(shù)
- 2 回答
- 0 關(guān)注
- 580 瀏覽
添加回答
舉報