通过反射设置其它进程窗口的属性和调用方法无效。
崔作非 2013-11-14 05:16:04 获取其它进程的属性值,可以得到。
Pvalue=TF.Reflection_Get_Property(@"..\..\..\WindowsFormsUI\bin\Release\" + filemainname + ".exe", "WindowsFormsUI.Form4", "Text", null);
public Object Reflection_Get_Property(string Path, string NameSpaceAndClassName, string PropertyName,object[] index)
{
Assembly Ass = Assembly.LoadFrom(Path);
Type TP = Ass.GetType(NameSpaceAndClassName);
PropertyInfo PI = TP.GetProperty(PropertyName);
object MeObj = System.Activator.CreateInstance(TP);
return PI.GetValue(MeObj, index);
}
类似的代码但设置值无效。
TF.Reflection_Set_Property(@"..\..\..\WindowsFormsUI\bin\Release\" + filemainname + ".exe", "WindowsFormsUI.Form4", "Text", "Form(4)", null);
public int Reflection_Set_Property(string Path, string NameSpaceAndClassName, string PropertyName, object value,object [] index)
{
Assembly Ass = Assembly.LoadFrom(Path);
Type TP = Ass.GetType(NameSpaceAndClassName);
PropertyInfo PI = TP.GetProperty(PropertyName);
object MeObj = System.Activator.CreateInstance(TP);
PI.SetValue(MeObj, value, index);
return 0;
}
反射调用其它窗口的方法,执行了但窗口没有效果。
TF.Reflection_Call_Method(@"..\..\..\WindowsFormsUI\bin\Release\" + filemainname + ".exe", "WindowsFormsUI.Form4", "TestAdd2", new object[]{100,30});
public int Reflection_Call_Method(string Path, string NameSpaceAndClassName, string MethodName, object[] Parameters)
{
Assembly Ass = Assembly.LoadFrom(Path);
Type TP = Ass.GetType(NameSpaceAndClassName);
MethodInfo MI = TP.GetMethod(MethodName);
object MeObj = System.Activator.CreateInstance(TP);
MI.Invoke(MeObj, Parameters);
return 0;
}
被反射调用的代码
public void TestAdd2(int a,int b)
{
int c;
c = a + b;
this.label1.Text = c.ToString();
this.label1.Show();//通过界面调用可以得到结果,通过反射调用窗口label未改变
Console.WriteLine(c);//通过反射调用可以得到结果
}
整个执行过程无异常报错,只是窗口的属性值未改变,求助大家,反射刚开始学,很多东东还不会,谢谢!