I want to get 'array type' of a type at run time. I do not need the instance of the array, just Type. I currently use the below method.
private Type GetArrayType(Type elementType)
{
return Array.CreateInstance(elementType, 0).GetType();
}
Is there any better solution without creating the instance of the array?
Note: I cannot use Type.GetType(elementType.FullName + "[]")
because I create the element Type at runtime by Reflection.Emit
. According to MSDN it requires dynamic assembly to be saved on disk which I do not want to do.
Yes, you can use Type.MakeArrayType
.
Returns a Type object representing a one-dimensional array of the current type, with a lower bound of zero.
private Type GetArrayType(Type elementType)
{
return elementType.MakeArrayType();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With