How to distinguish between types: Int32 [] and Int32 [*]?

Given the following code:

var type1 = typeof(int[]); // Int32[]
var type2 = Array.CreateInstance(elementType: typeof(int),
                                 lengths: new [] {0},
                                 lowerBounds: new []{1}).GetType(); // Int32[*]

Given the type of array (the type where .IsArray returns true), how can I reliably differ between these two types of arrays?

Without using any hacking solutions (for example, creating an instance or searching for "*" in the name).

Context: I am building a serializer and I need this to work for each type, so constant comparisons like == typeof (int []) will not work.

+4
source share
2 answers

, , , , , , , , Type.GetElementType() , . :

// Initialise our variables
object list1 = new int[5]; // Int32[]
object list2 = Array.CreateInstance(elementType: typeof(int),
                                    lengths: new[] { 0 },
                                    lowerBounds: new[] { 1 });
var type1 = list1.GetType();
var type2 = list2.GetType();

Debug.WriteLine("type1: " + type1.FullName);
Debug.WriteLine($"type1: IsArray={type1.IsArray}; ElementType={type1.GetElementType().FullName}; Is Int32[]: {type1 == typeof(Int32[])}");
Debug.WriteLine("type2: " + type2.FullName);
Debug.WriteLine($"type2: IsArray={type2.IsArray}; ElementType={type2.GetElementType().FullName}; Is Int32[]: {type2 == typeof(Int32[])}");

// To make this useful, lets join the elements from the two lists
List<Int32> outputList = new List<int>();
outputList.AddRange(list1 as int[]);
if (type2.IsArray && type2.GetElementType() == typeof(Int32))
{
    // list2 can be safely be cast to an Array because type2.IsArray == true
    Array arrayTemp = list2 as Array;
    // arrayTemp can be cast to IEnumerable<Int32> because type2.GetElementType() is Int32.
    // We have also skipped a step and cast ToArray
    Int32[] typedList = arrayTemp.Cast<Int32>().ToArray();
    outputList.AddRange(typedList);
}

// TODO: do something with these elements in the output list :)

:

type1: System.Int32[]
type1: IsArray=True; ElementType=System.Int32; Is Int32[]: True
type2: System.Int32[*]
type2: IsArray=True; ElementType=System.Int32; Is Int32[]: False
+2

:

var t1 = type1 == typeof(int[]); // true
var t2 = type2 == typeof(int[]); // false

, ?


, :

var tt1 = type1.GetConstructors().Length; // 1
var tt2 = type2.GetConstructors().Length; // 2

var ttt1 = type1.GetMembers().Length; // 47
var ttt2 = type2.GetMembers().Length; // 48
+1

Source: https://habr.com/ru/post/1673212/


All Articles