C# How to get the element type in the array type

Previously, there was a need to find the element type in the array. Finally, I did not find a suitable post on the Internet. I only found the following method

var type = typeof(int[]);
var elementType = Type.GetType(type.Name.Replace("[]", string.Empty));

Later, I finally felt that it was wrong. After printing the method of obtaining Type, I found that the instance method of GetElemetypeType() can obtain the element type in the array type.

var type = typeof(int[]);
var elementType = type.GetElementType();

This method can not only obtain the element type in the int[] type, but also obtain the multidimensional array int[,].

おすすめ

転載: blog.csdn.net/DoyoFish/article/details/127238991