Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a custom object array to System.Array in C#

Tags:

c#

.net

I have an array of custom objects. MyCustomArr[]. I want to convert this to System.Array so that I can pass it to a method that accepts only System.Array. The signature of the method is:

public void Load(Array param1, string param2)
{

}
like image 626
Nick Avatar asked Aug 05 '26 16:08

Nick


2 Answers

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");
like image 99
Fredrik Mörk Avatar answered Aug 10 '26 13:08

Fredrik Mörk


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());
    }
}

like image 33
JMarsch Avatar answered Aug 10 '26 13:08

JMarsch