Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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# 对象是数组或IEnumerable_C#_.net_Reflection - Fatal编程技术网

C# 对象是数组或IEnumerable

C# 对象是数组或IEnumerable,c#,.net,reflection,C#,.net,Reflection,我想知道是否有办法确定对象是数组还是IEnumerable,它比这更漂亮: var arrayFoo = new int[] { 1, 2, 3 }; var testArray = IsArray(arrayFoo); // return true var testArray2 = IsIEnumerable(arrayFoo); // return false var listFoo = new List<int> { 1, 2, 3 }; var testList = I

我想知道是否有办法确定对象是数组还是IEnumerable,它比这更漂亮:

var arrayFoo = new int[] { 1, 2, 3 };

var testArray = IsArray(arrayFoo);
// return true
var testArray2 = IsIEnumerable(arrayFoo);
// return false

var listFoo = new List<int> { 1, 2, 3 };

var testList = IsArray(listFoo);
// return false
var testList2 = IsIEnumerable(listFoo);
// return true


private bool IsArray(object obj)
{
    Type arrayType = obj.GetType().GetElementType();
    return arrayType != null;
}

private bool IsIEnumerable(object obj)
{
    Type ienumerableType = obj.GetType().GetGenericArguments().FirstOrDefault();
    return ienumerableType != null;
}
var arrayFoo=newint[]{1,2,3};
var testArray=IsArray(arrayFoo);
//返回真值
var testArray2=可枚举(arrayFoo);
//返回错误
var listFoo=新列表{1,2,3};
var testList=IsArray(listFoo);
//返回错误
var testList2=可枚举(listFoo);
//返回真值
私人布尔·伊萨雷(对象对象对象)
{
类型arrayType=obj.GetType().GetElementType();
返回arrayType!=null;
}
私有布尔可枚举(对象obj)
{
类型ienumerableType=obj.GetType().GetGenericArguments().FirstOrDefault();
返回ienumerableType!=null;
}
这有帮助吗

“是”关键字

检查对象是否与给定类型兼容

static void Test(object value)
{
    Class1 a;
    Class2 b;

    if (value is Class1)
    {
        Console.WriteLine("o is Class1");
        a = (Class1)o;
        // Do something with "a."
    } 
}
“as”关键字

尝试将值强制转换为给定类型。如果强制转换失败,则返回null

Class1 b = value as Class1;
if (b != null)
{
   // do something with b
}
参考

“是”关键字

“as”关键字

这有帮助吗

“是”关键字

检查对象是否与给定类型兼容

static void Test(object value)
{
    Class1 a;
    Class2 b;

    if (value is Class1)
    {
        Console.WriteLine("o is Class1");
        a = (Class1)o;
        // Do something with "a."
    } 
}
“as”关键字

尝试将值强制转换为给定类型。如果强制转换失败,则返回null

Class1 b = value as Class1;
if (b != null)
{
   // do something with b
}
参考

“是”关键字

“as”关键字


在C#中有一个
is
关键字:


C#中有一个
is
关键字:


这个问题有一个根本性的问题:数组是IEnumerable。您的
IsEnumerable
只检查泛型类型
MyAbstractClass
将返回true。您不测试项目是否为
IEnumerable
。它测试该类型是否为至少具有一个泛型参数的泛型类型。这个问题有一个基本问题:数组是IEnumerable。您的
IsEnumerable
仅检查泛型类型
MyAbstractClass
将返回true。您不测试项目是否为
IEnumerable
。它测试该类型是否为至少具有一个泛型参数的泛型类型。