-2

In C# is it possible to convert an array type to singular - for use with Activator.CreateInstance. Take this for example:

void Main()
{
    var types = new[] { typeof(ExampleClass), typeof(ExampleClass[]) };
    var objects = new List<object>();

    foreach (var type in types)
    {
        // possibly convert type here? (from array to singular - type[] to type) 

        Debug.WriteLine($"{type}");
        objects.Add(Activator.CreateInstance(type));
    }
}

// Define other methods and classes here

public class ExampleClass
{
    public int X;
    public int Y;
}

Gets the following output:

LINQPad output

wonea
  • 4,783
  • 17
  • 86
  • 139
  • 2
    What do you want to be returned? An array with zero elements? An array with one element? An instance of the element type of the array? Something else? – Matthew Watson Sep 20 '17 at 08:42
  • Correct me if I'm wrong, but you're code has little to do with your question. You're asking about converting an array type, but in code you're just trying to create a new array (using reflection), which is failing because you didn't provide a parameter to the array constructor - the size of the array you're trying to create. – decPL Sep 20 '17 at 08:45
  • Possible dupe? https://stackoverflow.com/q/3419456/982149 – Fildor Sep 20 '17 at 08:48

3 Answers3

1

If I understand your Question right you might want something like this using Type.GetElementType() via reflection.

static void Main(string[] args)
    {

        var types = new[] { typeof(ExampleClass), typeof(ExampleClass[]) };
        var objects = new List<object>();

        foreach (var type in types)
        {
            var typeInstance = type.GetElementType();

            if (typeInstance != null)
            {
                Debug.WriteLine($"{typeInstance}");
                objects.Add(Activator.CreateInstance(typeInstance));
            }
            else
            {
                objects.Add(Activator.CreateInstance(type));
            }
        }
    }

   public class ExampleClass
   {
        public int X;
        public int Y;
   }
MarkusE
  • 545
  • 4
  • 20
1

If I understand your question correctly, you want to get the base-type of an array, right? That should be quite easy with the IsArray property of the type, simply check each entry of your list like this:

private static Type GetTypeOrElementType(Type type)
{
    if (!type.IsArray)
        return type;

    return type.GetElementType();
}

Btw, if you want to create a new Array of that specific type, you can use Array.CreateInstance instead of Activator.CreateInstance

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
0

Found this works:

void Main()
{
    var types = new[] { typeof(ExampleClass), typeof(ExampleClass[]) };
    var objects = new List<object>();

    foreach (var type in types)
    {
        Debug.WriteLine($"{type}");
        objects.Add(type.IsArray
                    ? Activator.CreateInstance(type, 1)
                    : Activator.CreateInstance(type));
    }
}

// Define other methods and classes here

public class ExampleClass
{
    public int X;
    public int Y;
}
wonea
  • 4,783
  • 17
  • 86
  • 139