Answer the question
In order to leave comments, you need to log in
How to call class methods with parameters from xml file or array?
There is a class with methods that can take string variables as parameters, while the methods are different both in the number of parameters they take and in the return value. For example
obj["param1"] = xmlobj.SelectSingleNode["param2", nsmg].Atribute["param3"].Value;
obj["param4"] = class_method("param5");
string[,] param_mas =
{
{"param1", "param2", "param3"},
{....},
}
Answer the question
In order to leave comments, you need to log in
Organize in your code some structure / class in which you will store the name of the called method and its parameters. for example
public class SampleContainer
{
public string MethodName { get; set; }
public object[] Params { get; set; }
}
<Method>
<Name>Some_Method_Name</Name>
<Params>
<Param name = 'param1' value='value1'></Param>
<Param name = 'param2' value='value2'></Param>
</Params>
</Method>
using System;
using System.Reflection;
namespace ConsoleApplication2
{
public class Sample
{
public string SampleMethod(string param1, string param2)
{
return param1 + param2;
}
}
class Program
{
static void Main(string[] args)
{
Sample sample = new Sample();
var method = typeof(Sample).GetMethod("SampleMethod");
object[] param = new object[] {"Hello ","World!"};
string result = method.Invoke(sample, param) as string;
Console.WriteLine(result);
Console.ReadKey();
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question