2

I need to add the parameters and the values to the following object dynamically. So far, I do it statically (hardcoded) by creating the object as follows:

object parameters = new { Param1 = UserNumber, Param2 = "201403", Param3 = true };

Then in the function, pass it as:

Helper.create(parameters);

^This works.

But, sometimes I just need to pass just one, none, or more than those three. The amount can change dynamically and not necessarily be named the same or it can be in a different order. So, I can't leave the name of the variables Param1, Param2 and Param3 statically. I also, don't control the behavior for the 'create()' function.

I tried this (and more):

List<Tuple<object, object>> tupleList = new List<Tuple<object, object>>();

tupleList.Add(new Tuple<object, object>("Number", UserNumber));
tupleList.Add(new Tuple<object, object>("Start", 201403));
tupleList.Add(new Tuple<object, object>("Show", true));

object tuptest = tupleList.Cast<object>().ToArray();

But it creates it as one array with objects inside. When you go step by step in the debugging, they show up differently than what I need.

The Documentation example is:

Helper.create( new { Parameter1 = "Text", Parameter2 = 1000 } );

How can I create this dynamically?

4
  • 5
    What is create expecting to receive? Commented Apr 28, 2015 at 14:06
  • do you have control over the create method? if so, maybe pass a dictionary in stead. Commented Apr 28, 2015 at 14:09
  • This question is very unclear. Please try to edit it so that someone other then yourself can understand it Commented Apr 28, 2015 at 14:10
  • edited to explain more Commented Apr 28, 2015 at 14:19

3 Answers 3

2

Declare the method's signature using params object[] args, or a more specific type. This will allow you to pass any (or none) amount of parameters to the method, which you can then iterate inside the method.

E.g.:

public void Foo(params Tuple<object, object>[] args)

var a = new Tuple<object, object>("Number", UserNumber);
var b = tupleList.Add(new Tuple<object, object>("Start", 201403));
var c = tupleList.Add(new Tuple<object, object>("Show", true));

You'd then call it using Foo(a, b, c);

Sign up to request clarification or add additional context in comments.

Comments

1

Why not creating a whole new specific type?

public class UserParameters {
     public UserNumber Number { get; set; }
     public string Start { get; set; }
     public bool Show { get; set; }
}

This way, you have compile-time errors if you mistype anything, and also compile-time type checking. You don't have to cast your generic object arguments to any type, which was very error-prone.

Also, you can dynamically add or remove properties, so in a way you are adding or removing parameters.

The create method can accept this object, and you would call it like this:

parent.create(new UserParameters() {
     Number = Session.UserNumber,
     Start = "201403",
     Show = true
});

Any property that is null is a parameter you didn't pass or defined.

1 Comment

This won't work. I don't know the parameter names or types before hand.
1

You can create an ExpandoObject & pass it to create

var x = new ExpandoObject() as IDictionary<string, Object>;
            x.Add("Number", "UserNumber");
            x.Add("Start", 201403);
            x.Add("Show", true);
            Create(x);

1 Comment

This is going in the right direction. I now have 3 objects in an array. But it creates a different type of object. I added x = x.Cast<object>().ToArray(); but still I got 3 items. Seems it is looking for just one line in the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.