[英]Convert a custom object array to System.Array in C#
I have an array of custom objects. 我有一系列自定义对象。 MyCustomArr[].
MyCustomArr []。 I want to convert this to System.Array so that I can pass it to a method that accepts only System.Array.
我想将它转换为System.Array,以便我可以将它传递给只接受System.Array的方法。 The signature of the method is:
该方法的签名是:
public void Load(Array param1, string param2)
{
}
No conversion is needed for that as far as I know. 据我所知,不需要转换。 You can simply go ahead and pass your array to the method.
您可以直接将数组传递给方法。 The following code works out well:
以下代码运作良好:
MyClass[] myClassArray = new MyClass[2];
myClassArray[0] = new MyClass();
myClassArray[1] = new MyClass();
Load(myClassArray, "some text");
What do you want to do with the array? 你想对阵列做什么? The code below builds and runs, so I'm not sure where your problem lies:
下面的代码构建并运行,所以我不确定你的问题在哪里:
public class MyClass
{
public class MyObject
{
}
public static void RunSnippet()
{
MyObject[] objects = new MyObject[5];
Test(objects);
}
private static void Test(System.Array obj)
{
System.Console.WriteLine("Count: " + obj.Length.ToString());
}
}
你应该能够施展它,但我认为这将隐式为你完成。
System.Array array = (System.Array)(new int[] { 1, 2, 3, 4 });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.