Adnand
Adnand

Reputation: 572

How to pass parameters as a single object?

I have this example to tell you what I'm looking for:

private void myMethod(string a = "", string b = "", string c = "")
{
   // do things
}

I want to find a way where I can call that method like this:

MyParameterObject parameters = new MyParameterObject();
// b is the name of parameter
parameters.AddParameter("b", "b_value");
parameters.AddParameter("c", "c_value");
myMethod(parameters);

Upvotes: 4

Views: 1239

Answers (4)

Janne Matikainen
Janne Matikainen

Reputation: 5121

You could use a dictionary like suggested and make a method invoker to build the parameter array from the dictionary and invoke the method from the instance you pass in.

public static class Invoker
{
    public static void Invoke<TClass>(string methodName, Dictionary<string, object> myParameterObject, TClass instance)
    {
        var method = typeof(TClass).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
        var parameters = method.GetParameters().Select(parameter => myParameterObject.ContainsKey(parameter.Name) ? myParameterObject[parameter.Name] : null).ToArray();

        method.Invoke(instance, parameters);
    }
}

And call it like

var parameters = new Dictionary<string, object> { { "b", "b_value" }, { "c", "c_value" } };

var myClass = new MyClass(); 
Invoker.Invoke("MyMethod", parameters, myClass);
Console.ReadKey();

Assuming your MyClass would contain the MyMethod

public class MyClass
{
    private void MyMethod(string a = "", string b = "", string c = "")
    {
        Console.WriteLine("a : " + a + " b : " + b + " c : " + c);
    }
}

Though this is very error prone but useful as an exercise for reflection skills.

Upvotes: 2

Nico
Nico

Reputation: 1803

As described by various others we don't really know what your problem is but here are two possibilities.

First: Using a Dictionary

Dictionary<string,string> paramDictionary = new Dictionary<string,string>();
paramDictionary.Add("b", "b_value");
paramDictionary.Add("c", "c_value");
myMethod(paramDictionary);

Second: Creating a Object that suits your needs

public class ParameterObject{

  private string firstName, lastName;
  public string FirstName { get { return firstName; } set { firstName = value; } }
  public string LastName { get { return lastName; } set { lastName = value; } }    
  // and so on

  public ParameterObject(){
    // leave it empty or create it with parameters
  }

}

and then just use it like in your example:

ParameterObject parameters = new ParameterObject();
parameters.setFirstName("b_value");
parameters.setLastName("c_value");
myMethod(parameters);

Since you defined this Object you can use it in the method like this:

myMethod(ParameterObject parameter){

String firstName = parameter.getFirstName();
String lastName = parameter.getLastName();

}

Upvotes: 2

Yogesh
Yogesh

Reputation: 685

Create Key Value Pair and pass it to the function

List<KeyValuePair<string, string>> kvpList = new List<KeyValuePair<string, string>>()
{
    new KeyValuePair<string, string>("a", "a_value"),
    new KeyValuePair<string, string>("b", "b_value"),
    new KeyValuePair<string, string>("c", "c_value"),
};


public void myMethod(KeyValuePair<string, string>[] pairs)
{
    // Read key value pair
    // ...
}

If properties are known then create class and assign properties to it.

Upvotes: 3

sujith karivelil
sujith karivelil

Reputation: 29026

If all the parameter values required in the method are of same type(let it be string) then you can pass the parameter as a Dictionary like the following:

private void myMethod(Dictionary<string,string> paramDictionary)
{
   // do things
}

So that you can call the method like this:

Dictionary<string,string> paramDictionary = new Dictionary<string,string>();
paramDictionary.Add("b", "b_value");
paramDictionary.Add("c", "c_value");
myMethod(paramDictionary);

Upvotes: 6

Related Questions