传递一个对象[]到一个params对象[]不工作 [英] passing a object[] to a params object[] not working
问题描述
我已经阅读了有关将对象[]传递给params对象[]的主题,但我不知道为什么它不与我一起工作。
I've read the topic about passing an object[] to a params object[] but I don't know why it's not working with me.
在类中有这些太多的函数:
I have these too functions in a class:
...
private void CallbackEvent(object source, CallbackEvetArgs e) { // Some event with e.Data as string
...
string[] values = e.Data.Split('|');
DoSave("save", values.Skip(1).Cast<object>().ToArray());
...
}
...
public void DoSave(string action, params object[] values) {
...
string value1 = values[0];
...
}
...
接收一个字符串value1,value1接收整个数组(string []),因此无效的转换异常。
but instead of receiving an string in value1, value1 is receiving the whole array (string[]) and therefore an invalid casting exception.
我做错了什么?
推荐答案
C#(一般为.NET)数组是协变的。您可以简单地将 string []
传递给 object []
参数。
C# (.NET in general) arrays are covariant. You can simply pass the string[]
to an object[]
parameter.
DoSave("save", values.Skip(1).ToArray());
您发布的代码绝对不是您测试的确切代码。 Cast< object>
也应该正常工作。此行不应编译:
The code you posted is definitely not the exact code you tested. Cast<object>
should also work correctly. This line shouldn't compile:
string value1 = values[0]; // object -> string, no implicit conversion.
请发布导致问题的完全代码。
Please post the exact code causing the problem.
这篇关于传递一个对象[]到一个params对象[]不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!