I've found lots of info on passing parameters to methods and found that having the method use params Object[] args
to be a great (and sometimes only) way to do this.
我已经找到了很多关于将参数传递给方法的信息,并发现使用params对象[] args是一种很好的(有时只是)方法。
i.e. sample method:
即样本方法:
public void someMethod(params object[] args)
{
//do something
}
But when passing the parameter object like this to a Com Method, it doesn't work:
但是当像这样将参数对象传递给Com方法时,它不起作用:
object[] passParameters = new object[2];
passParameters[0] = "test1";
passParameters[1] = "test2";
//Calling method compiled with with CodeDom
MethodInfo method = classObject.GetType().GetMethod("someMethod");
object dynamicOutput = method.Invoke(classObject, passParameters);
For it to work, I have found that I need to pass the parameter object as a new Object:
为了使它工作,我发现我需要将参数对象作为新对象传递:
object dynamicOutput = method.Invoke(classObject,(new object[] { passParameters });
Can anyone please explain to me why I need to do this and what the reasoning is behind it? Thanks.
任何人都可以向我解释为什么我需要这样做以及它背后的原因是什么?谢谢。
2 个解决方案
#1
0
They don't. You can just use them as
他们没有。您可以将它们用作
someMethod("test1", "test2", ... )
BUT. If you're creating an array beforehand, it has to be passed differently, as you encountered
但。如果您事先创建了一个数组,则必须以不同的方式传递它,就像您遇到的那样
#2
0
I've come to realise that the original passParameters
object in the example contains two elements, as we know:
我已经意识到示例中的原始passParameters对象包含两个元素,我们知道:
passParameters[0] = "test1";
passParameters[1] = "test2";
However, when the array is passed to the Com method, it requires being passed as so:
但是,当数组传递给Com方法时,它需要像这样传递:
new object[] { passParameters })
My understanding of this is, that it creates a new object array which has put the existing 'passParameters' array into its first element [0] (effectively creating a new 2d array):
我对此的理解是,它创建了一个新的对象数组,它将现有的'passParameters'数组放入其第一个元素[0]中(有效地创建了一个新的2d数组):
passParameters[0][0] = "test1";
passParameters[0][1] = "test2";
The method MethodInfo
class in my question, required a single object passed to the Invoke
method, which meant that the original 'passParameters' object with its two elements had too many arguments. By passing it inside a new object, this passes all of the elements as a single object, but containing an array of those parameters.
在我的问题中方法MethodInfo类,需要一个传递给Invoke方法的对象,这意味着原始的'passParameters'对象及其两个元素有太多的参数。通过将其传递到新对象中,它将所有元素作为单个对象传递,但包含这些参数的数组。
#1
0
They don't. You can just use them as
他们没有。您可以将它们用作
someMethod("test1", "test2", ... )
BUT. If you're creating an array beforehand, it has to be passed differently, as you encountered
但。如果您事先创建了一个数组,则必须以不同的方式传递它,就像您遇到的那样
#2
0
I've come to realise that the original passParameters
object in the example contains two elements, as we know:
我已经意识到示例中的原始passParameters对象包含两个元素,我们知道:
passParameters[0] = "test1";
passParameters[1] = "test2";
However, when the array is passed to the Com method, it requires being passed as so:
但是,当数组传递给Com方法时,它需要像这样传递:
new object[] { passParameters })
My understanding of this is, that it creates a new object array which has put the existing 'passParameters' array into its first element [0] (effectively creating a new 2d array):
我对此的理解是,它创建了一个新的对象数组,它将现有的'passParameters'数组放入其第一个元素[0]中(有效地创建了一个新的2d数组):
passParameters[0][0] = "test1";
passParameters[0][1] = "test2";
The method MethodInfo
class in my question, required a single object passed to the Invoke
method, which meant that the original 'passParameters' object with its two elements had too many arguments. By passing it inside a new object, this passes all of the elements as a single object, but containing an array of those parameters.
在我的问题中方法MethodInfo类,需要一个传递给Invoke方法的对象,这意味着原始的'passParameters'对象及其两个元素有太多的参数。通过将其传递到新对象中,它将所有元素作为单个对象传递,但包含这些参数的数组。