Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何区分类型:Int32[]和&;Int32[*]?_C#_Reflection_Types - Fatal编程技术网

C# 如何区分类型:Int32[]和&;Int32[*]?

C# 如何区分类型:Int32[]和&;Int32[*]?,c#,reflection,types,C#,Reflection,Types,给定以下代码: var type1 = typeof(int[]); // Int32[] var type2 = Array.CreateInstance(elementType: typeof(int), lengths: new [] {0}, lowerBounds: new []{1}).GetType(); // Int32[*] 给定一个数组类型(其

给定以下代码:

var type1 = typeof(int[]); // Int32[]
var type2 = Array.CreateInstance(elementType: typeof(int),
                                 lengths: new [] {0},
                                 lowerBounds: new []{1}).GetType(); // Int32[*]
给定一个数组类型(其中.IsArray返回true的类型),如何可靠地区分这两种数组类型

最好不使用任何黑客解决方案(例如实例化类型或在名称中查找“*”)


上下文:我正在构建一个序列化程序,我需要它为存在的每种类型工作,因此像==typeof(int[])这样的常量比较将不起作用。

如果值类型已知:

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

检查类型是否未通过比较是一个有效的选项,但是,如果要检查类型的特定属性,例如,要知道将其强制转换为哪种类型的数组,可以使用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 :)

像这样的问题需要更多的上下文,我永远不会建议对类型名使用字符串比较,但对您的问题的明显答案是type2!=typeof(int[])。如果我们有更多关于这些类型或变量来源的上下文(只是一些简单的信息),那么我们可能会给您一些真正有用的信息answers@PetSerAl这是一个绝妙的回答!非常感谢你,我想再试试Chris Schaller我正在构建一个序列化程序,我需要它来处理每种类型,所以对typeof(int[])的常量检查不起作用,我将把它添加到我的问题中。发现得很好!不过,这可能会在CLR的未来版本中发生变化,所以我将使用PetSerAl的解决方案,谢谢!
type1: System.Int32[]
type1: IsArray=True; ElementType=System.Int32; Is Int32[]: True
type2: System.Int32[*]
type2: IsArray=True; ElementType=System.Int32; Is Int32[]: False