开发者

Calling function dynamically by using Reflection

I'm generating dll files contain code like the following example :

using System;
using System.Collections;
using System.Xml;
using System.IO;
using System.Windows.Forms;

namespace CSharpScripter
{

public class TestClass : CSharpScripter.Command
{
    private int i=1;
    private int j=2;
    public int k=3;

    public TestClass6()
    {

    }

    public void  display (int i,int j,int k)
    {
        string a = null;
        a= k.ToString();

        string a1 = null;
        a1= this.i.ToString();

        string a2 = null;
        a2= j.ToString();

        MessageBox.Show(" working! "+ "k="+ a +" i="+a1 + " j="+ a2);
    }

    public void setValues(int i,int j,int k1)
    {
        this.i=i;
        this.j=j;
        k=k1;
    }

I'm compiling the previous cod开发者_如何学运维e, then I execute an object from the dll file. So, in the second part of the code ( Executing part), I'm just calling the execute function, It contains a call for a function, I named here: display.

For that I need to set values in the declaration by a setValue function. I want it to be called dynamically (setValues ), which has declaration like :

public void(Parameter[] parameters)
{
    //some code block here
}

For this situation I used Reflection.

Type objectType = testClass.GetType();
MethodInfo members = objectType.GetMethod("setValues");

ParameterInfo[] parameters = members.GetParameters();

for( int t = 0; t < parameters.Length; t++)
{
     if (parameters[t]. ParameterType ==  typeof())
     {
          object value = this.textBox2.Text;
          parameters.SetValue(Convert.ChangeType(value,parameters[t].ParameterType), t);                                    
     }
}

But it throws an casting error" Object cannot be stored in an array of this type." at last line, in first parameter for (setValue) method. What is the problem here?

And how I can call the method Dynamically after the previous code, by( Invoke) or is there a better way?

Thanks.


The parameters variable is an array of ParameterInfo. So each element is of type ParameterInfo, not of the type of the corresponding parameter. It is to be expected that you cannot cast a string (the result type of the Text property) to a ParameterInfo.

You need to create an object array, of the same size as ParameterInfo and build it in the for loop. Then you can use the MethodInfo instance and call Invoke with this object array.


Thanks for your answers. The unnamed is setValue, I wrote the example code here:

public void setValues(int i,int j,int k1)
{
    this.i=i;
    this.j=j;
    k=k1;
}

I just want to show you the declaration :)

public void(Parameter[] parameters)
{
    //some code block here
}

Anyway, we can forget this part of unnamed function, just focus on this code :

Type objectType = testClass.GetType();                     
MethodInfo members = objectType.GetMethod("setValues");
ParameterInfo[] parameters = members.GetParameters();
For( int t = 0; t < parameters.Length; t++)
{
  If (parameters[t]. ParameterType ==  typeof())
  {
    object value = this.textBox2.Text;                          
    parameters.SetValue(Convert.ChangeType(value,parameters[t].ParameterType), t);
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜