samedi 11 septembre 2021

Activate type with generic params array set to null

Let's say I have this ctor:

public MyType<T>(int myInt, string myString, params T[] myObjects) { }

I could set the params array itself to null (rather than pass a null into the array):

MyType(1, "foo", (T[])null)

Let's say I want to do that dynamically. I tried this:

var args = new object[] { 1, "foo", null };
var myType = Activator.CreateInstance(typeof(MyType), BindingFlags.Instance | BindingFlags.NonPublic, null, args, null);

But that throws a MissingMethodException.

If I change the ctor's params T[] to params object[] then it works, but I prefer to keep the generic form.

How can I do this?





Aucun commentaire:

Enregistrer un commentaire