I am doing a simple program in Silverlight to inkove javascript function in silverlight.
The silverlight function is as under
void InvokeJS(params object[] items)
{
object result = System.Windows.Browser.HtmlPage.Window.Invoke("JSFunction", items);
}
Pasing value to this function is happening as under
InvokeJS((object)new object[]{ (object)"10", (object)"20"})
And the JS function is as under
function JSFunction(params) {
alert(params);
}
Now how to read the params value in javascript?
The params variable is just the first of many arguments being passed in. You can access the other arguments using the following syntax:
alert(this.arguments[0]);
alert(this.arguments[1]);
alert(this.arguments[2]);
If you're passing all the arguments in a single variable, it will be an array so use:
alert(params[0]);
alert(params[1]);
alert(params[2]);
To the called function, the params array is just that, an array.
In this case you will have an array that looks like this:
[ [ "10", "20" ] ]
I got it
alert(params[0]); alert(params[1]);
Related
After learning how to Create a method with infinite parameters, I wonder is it legal to store the parameter array into a array. Will it cause any problem, since I don't see many people use this approach.
Code below :
class Foo
{
private String[] Strings;
public Foo(params String[] strings)
{
Strings = strings;
}
...
}
That's fine - it's just an array.
All the compiler does with a parameter array is convert a call like this:
Foo("x", "y");
into:
Foo(new string[] { "x", "y" });
That's really all there is to it. Anything you'd expect to be appropriate with the second call is fine with a parameter array.
Arrays passed into public methods are rarely suitable to store directly due to all arrays being mutable - but that's a matter of how you handle mutable parameter types rather than being specific to parameter arrays.
When I'm passing the values in Fiddler like {"rajehs","ramesh","ramsgkfhh"}, I'm getting all the values in [0] location. I've also tried by using = {"rajehs","ramesh","ramsgkfhh"}.
I declared the method like :
public void Post([FromBody]string[] value)
{
}
You are passing a JSON object in our body, instead of an array. Use [] to pass a JSON array:
["rajehs","ramesh","ramsgkfhh"]
This question already has answers here:
Calling a function using reflection that has a "params" parameter (MethodBase)
(3 answers)
Closed 8 years ago.
Trying to invoke a method with an arbitrary number of parameters:
Type objType = obj.GetType();
MethodInfo method = objType.GetMethod("InvokedMethod");
method.Invoke(obj, new string[] { "param1", "param2" });
The method signature looks like that:
public void InvokedMethod(params string[] args) { ... }
Why I get following Exception:
System.Reflection.TargetParameterCountException (Parameter count mismatch)
The method doesn't accept two parameters, it accepts one parameter that is an array. The compiler will perform a transformation such that a method call of two strings will be transformed into a call of a single array with two values. That transformation won't be done for you when using reflection. You'll need to explicitly create an array and put the two values in it:
method.Invoke(obj, new object[] { new[]{"param1", "param2"} });
Remember that Invoke doesn't accept a single value of that one parameter either. It accepts an array of all parameters. Passing new string[] { "param1", "param2" } to Invoke is telling Invoke that you have two parameters, each of which are strings. You need to wrap your one array parameter in another array so that Invoke sees that you have one parameter that is itself an array.
Try this:
method.Invoke(obj, new object[] {new string[] {"param1", "param2"}});
.Invoke takes an array of arguments that represents the entire signature. in your case the first argument should be an array of strings. So you have to pass in an array of objects with the first element being the array of strings.
I am calling a function which takes the same form as string.format where the first parameter is a string and the remainder are the replacement values. I have the string in a variable and the replacement values in an array, how can I call this function given any number of objects in the array? Simply passing in the array as the last argument does not work.
Use the params keyword:
public string MyMethod(string value, params object[] args)
{
// as an example
return string.Format(value, args);
}
Then you can call it either with individual values
MyMethod("Test", "value1", "value2");
Or with an array
MyMethod("Test", new [] { "value1", "value2" });
you need to use the params keyword
The function I was calling had the signature
public static IQueryable Where(this IQueryable source, string predicate, params object[] values)
Thhe issue was that I was passing in an array of ints as the last parameter. When I createed a new array of objects poplulated from that initial array and passed it in it worked. Thanks for the answers
The following code sample prints:
T
T[]
T[]
While first two lines are as expected, why compiler selected param array for a regular array?
public class A
{
public void Print<T>(T t)
{
Console.WriteLine("T");
}
public void Print<T>(params T[] t)
{
Console.WriteLine("T[]");
}
}
class Program
{
static void Main(string[] args)
{
A a = new A();
a.Print("string");
a.Print("string","string");
a.Print(new string[] {"a","b"});
}
}
Under the hood
a.Print("string","string");
is just syntactic sugar for
a.Print(new string[]{"string","string"});
EDIT: Like I said, the params keyword only automagically creates the array for you, you tell the compiler: either accept an array of T directly or use the X input params to construct that array.
It addition to what others have said, the params keyword also causes a ParamArrayAttribute to the generated for array parameter. So, this...
public void Print<T>(params T[] t) { }
Is generated by the compiler as...
public void Print<T>([ParamArray] T[] t); { }
It is that attribute which indicates to the compiler and the IDE that the method can be called using simpler syntax...
a.Print("string", "string");
rather than...
a.Print(new string[] { "string", "string" });
I think this actually has more to do with type inference than with the params keyword. The inference engine assumes on the third line that the type of T is string[] and therefore passes it to the first method.
try Console.WriteLine(typeof(T)) if you don't believe me
when having the params keyword, the compiler will do a little bit of translation for the formal function declaration, as well as the actual function call.
Formal function declaration:
Under the hood, the IL will be translated to essentially the same as
public void Print<T>(T[] array);
Except, when compiling, the actual function call will be checked for syntax translation. Meaning,
a.Print("string1", "string2");
Becomes the same IL code as
a.Print(new string[]{"string1", "string2"});
That is why line 2 and 3 are the same output, because under the hood, they got translated to the exact same IL.
Question about why line 3 is not print "T" is because, .NET compiler will always try to find the best overloaded match, and so both line 2 and 3 called to the T[] version instead of the plain T.
Exactly as arul said. If you open up the project in reflector, you'll see the following:
a.Print<string>(new string[] { "string", "string" });
Params allows you to pass multiple objects of the same type. it is a shortcut way of passing array