remove MakeGenericType()
and it will start to work, use this
b[i] = Activator.CreateInstance(a[i].GetType());
MakeGenericType is for Substituting generic parameters with defined ones.
As example if your array would contain List<T>
, call of a[i].GetType().MakeGenericType(typeof(int))
will return type List<int>
.
In your case I don't see any generic type, so I wonder why you use it
Also note that calling Activator.CreateInstance
like so will work only if type have parameterless constructor, which is not true for example in case of string
. You can handle string
case like this
for (int i = 0; i < a.Length; i++)
{
var oType = a[i].GetType();
if (oType == typeof(string))
b[i] = string.Empty; //or may be null
else
b[i] = Activator.CreateInstance(oType);
}
Something simular should be done for all types without parameterless constructor.
Activator.CreateInstance(type, parameters...)
can do the job.
You can use reflection to investigate type constructor paramaters if needed