mercredi 4 octobre 2017

Creating an initialising an array of a reflected type in a single statement

Is it possible to create and initialise an array of a reflected type in .NET in a single statement?

ie the equivalent of doing this, but using reflection:

var myArray = new String[] {"Test"};

I'm after an Array of a specific type, rather than an array of object.

Because Array lacks a constructor that can initialise values, I've been trying to use a generic List. This is what I've got so far, which doesn't work (no matching constructor can be found):

C#:

var myArray = (Activator.CreateInstance(   typeof(List<>).MakeGenericType(typeof(string)),
                                                System.Reflection.BindingFlags.CreateInstance
                                            |   System.Reflection.BindingFlags.Public
                                            |   System.Reflection.BindingFlags.Instance
                                            |   System.Reflection.BindingFlags.OptionalParamBinding,
                                            null,
                                            new[] {
                                                    (new[]   {Activator.CreateInstance( typeof(string),
                                                                                        System.Reflection.BindingFlags.CreateInstance
                                                                                    |   System.Reflection.BindingFlags.Public
                                                                                    |   System.Reflection.BindingFlags.Instance
                                                                                    |   System.Reflection.BindingFlags.OptionalParamBinding,
                                                                                        null,
                                                                                        new[] { "Test".ToCharArray() },
                                                                                        null)
                                                            }).AsEnumerable()
                                                    },
                                            null));

VB.NET:

Dim myArray = Activator.CreateInstance(     GetType(List(Of)).MakeGenericType(GetType(String)),
                                                Reflection.BindingFlags.CreateInstance _
                                            Or  Reflection.BindingFlags.Public _
                                            Or  Reflection.BindingFlags.Instance _
                                            Or  Reflection.BindingFlags.OptionalParamBinding,
                                            Nothing,
                                            {
                                                {Activator.CreateInstance(  GetType(String),
                                                                            Reflection.BindingFlags.CreateInstance _
                                                                        Or  Reflection.BindingFlags.Public _
                                                                        Or  Reflection.BindingFlags.Instance _
                                                                        Or  Reflection.BindingFlags.OptionalParamBinding,
                                                                            Nothing,
                                                                            {"Test".ToCharArray()}, Nothing)
                                                }.AsEnumerable()
                                            },
                                            Nothing)

This question is asked out of curiosity about what is possible, rather than what should be done (as in I don't care if the solution is hideous, this is just for fun). I'll happily accept answers for either language, if this is possible!





Aucun commentaire:

Enregistrer un commentaire