Hans Rudel
Hans Rudel

Reputation: 3611

Optimizer, how one method can know how many parameters are in another

Info: Apologies in advance as im not sure if my explanation will be sufficient. Im trying to get my head around how a genetic algorithm would be written. Below explanation is the general part im not sure about which someone with a reasonable amount of c# experience should be able to answer as its not GA specific knowledge.

Question: I need to write a method (Manager_Method) whose main purpose is to pass in different combination of parameters to a second method (Computation_Method) which, using those input values, returns a result.

The tricky part comes with the fact that Manager_Method needs to be able to determine what parameters are required for the selected Computation_Method (There will be several Computation_Methods each with a different number/type of inputs).

So im wondering if someone knows of some slick way that to get around this, bar having to hard code in to an array, what the inputs are for each Computation_Method. Which would require a recompile each time i added a new Computation_Method.

Thanks in advance.

Edit: XML might be an idea. The thing im trying to get away from is hard coding in the parameters. What im trying to avoid is recompiling in the event i added a new Computation_Method AND also somehow get allow the Manager method to know what parameters are required for each Computation_Method. I figured there would be some way of doing this.

Upvotes: 0

Views: 102

Answers (2)

Sergey Rybalkin
Sergey Rybalkin

Reputation: 3026

Not sure I understand your question but looks like you can use Reflection here. In particular MethodInfo class and its GetParameters method. Here is how I would do it:

public class A
{
    public void MyMethod(int integer, string str)
    {
    }
}

void Main()
{
    System.Reflection.MethodInfo method = typeof(A).GetMethod("MyMethod");
    ParameterInfo[] pars = method.GetParameters();
    foreach (ParameterInfo p in pars) 
    {
        Console.WriteLine(p.Name + ": " + p.ParameterType);
    }
}

Upvotes: 1

Tabish Sarwar
Tabish Sarwar

Reputation: 1525

Well i dont know what parameters those can be but with the limited information you gave.I think you can can populate a dictionary in your ManagerMethod and pass it onto the Computation_Method where you can check the key/valuepair. The key would be your paramtername and value would be your parameter value to pass in .

e.g:

private vioid ManagerMethod()
{
   Dictionary<string,string>  params = new Dictionary<string,string>();
   params.Add("param1","Value1");
    params.Add("param2","Value2");
    params.Add("param3","Value3");

   ComputationMethod(params);
}
private void ComputationMethod(Dictionary<string,string> param)
{ 
   //Iterate through the Dictioanry
   // Check for your Key/Value and performa accoudringly.
}

Based on the same idea . Now you can put your params in XML file as well. read them and pass them. You can also create a List of Objects and pass that to your methods.

I think this can help you in coming up with something.

Upvotes: 1

Related Questions