2

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)
{

}
1
  • You have not stated what you tried, and what the results of those attempts were. Did you get any compile errors? run-time exceptions? other helpful information. I think there is more to your question than you wrote. But unless you provide detailed, helpful information, no one here can do much for you. Commented Jun 25, 2009 at 17:54

3 Answers 3

8

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");
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

You should just be able to cast it, but I think this will be done implicitly for you.

System.Array array = (System.Array)(new int[] { 1, 2, 3, 4 });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.