C# 如何观察数组的值类型?

C# 如何观察数组的值类型?,c#,reflection,C#,Reflection,我有一种方法可以检查对象的类型,以确定其是否复杂: private static bool IsComplexObject(Type type) { if (IsNullable(type)) { // nullable type, check if the nested type is simple return IsComplexObject(type.GetGenericArguments()[0]); } if (type

我有一种方法可以检查对象的
类型
,以确定其是否复杂:

private static bool IsComplexObject(Type type)
{
    if (IsNullable(type))
    {
        // nullable type, check if the nested type is simple
        return IsComplexObject(type.GetGenericArguments()[0]);
    }

    if (type.Equals(typeof(string)))
    {
        return false;
    }
    if (type.Equals(typeof(decimal)))
    {
        return false;
    }
    if (type.Equals(typeof(DataTable)))
    {
        return false;
    }
    if (type.IsValueType)
    {
        return false;
    }
    if (type.IsPrimitive)
    {
        return false;
    }
    if (type.IsEnum)
    {
        return false;
    }

    return true;
}
问题是:当我有一个简单类型数组的
类型时,
Int32[]
例如,我的方法返回
true

我可以通过在方法中添加以下
if
语句来防止这种情况发生:

if (type.IsArray)
{
    return false;
}
问题是这个
if
语句会阻止识别实际的复杂对象。例如,以下设置将自定义类确定为不复杂:

public class TestClass
{ 
    public void TestComplexArray()
    {
        var result = IsComplexObject(typeof(MyComplexClass[]));

        // result == false
    }
}

public class MyComplexClass
{
    public string Name { get; set; }
    public string Id { get; set; }
}

因此,我的问题是:如何检查数组值类型的复杂性,以将
Int32[]
MyComplexClass[]
分开?

也许您想检查数组秩,并在元素类型上重复出现

if (type.IsArray)
{
    if (type.GetArrayRank() != 1)
    {
        return true;
    }

    Type elementType = type.GetElementType();

    if (elementType.IsArray)
    {
        return true;
    }

    return IsComplexType(elementType);
}

也许您想检查数组秩,并在元素类型上重复出现

if (type.IsArray)
{
    if (type.GetArrayRank() != 1)
    {
        return true;
    }

    Type elementType = type.GetElementType();

    if (elementType.IsArray)
    {
        return true;
    }

    return IsComplexType(elementType);
}

尝试检索元素类型,然后递归调用
IsComplexObject
,如下所示:

if (type.IsArray) return IsComplexObject(type.GetElementType());
private static bool IsComplexObject(Type type, bool recurse = true)
{
    if (type.IsArray) return 
    (
        recurse
        ? IsComplexObject(type.GetElementType(), false) 
        : false
    );
    //etc.
对于“复杂对象”(不符合代码中指定的条件的对象)数组,这应该返回true。请注意,对于复杂对象数组或数组的数组,它也将返回true。如果这是一个问题,您可以进行更改,使其仅递归一次,如下所示:

if (type.IsArray) return IsComplexObject(type.GetElementType());
private static bool IsComplexObject(Type type, bool recurse = true)
{
    if (type.IsArray) return 
    (
        recurse
        ? IsComplexObject(type.GetElementType(), false) 
        : false
    );
    //etc.

尝试检索元素类型,然后递归调用
IsComplexObject
,如下所示:

if (type.IsArray) return IsComplexObject(type.GetElementType());
private static bool IsComplexObject(Type type, bool recurse = true)
{
    if (type.IsArray) return 
    (
        recurse
        ? IsComplexObject(type.GetElementType(), false) 
        : false
    );
    //etc.
对于“复杂对象”(不符合代码中指定的条件的对象)数组,这应该返回true。请注意,对于复杂对象数组或数组的数组,它也将返回true。如果这是一个问题,您可以进行更改,使其仅递归一次,如下所示:

if (type.IsArray) return IsComplexObject(type.GetElementType());
private static bool IsComplexObject(Type type, bool recurse = true)
{
    if (type.IsArray) return 
    (
        recurse
        ? IsComplexObject(type.GetElementType(), false) 
        : false
    );
    //etc.

你对“复杂类型”的定义是什么?(我在维基百科上只找到了分词定义)。你是说所有的课程吗?好的,任何类都可以被赋予索引器,因此它的行为就像一个数组(),所以它实际上不是一个简单的排除。数组确实是类。在我的
IsComplexObject
方法中概述了我对复杂类的定义。除了数组类型外,该方法可以准确地识别我对复杂对象的定义。你试过了吗?这已经够疯狂了,它可能会奏效。应该能帮你做你想做的事我不知道你想要这个做什么。。。我特别想知道为什么所有的值类型都被认为不复杂,因为它们实际上可能非常复杂,并且包含许多嵌套项。我的意思是,我不是100%确定你真的想把
public struct MyStruct{public MyComplexClass Stuff{get;set;}
算作非复杂类型(虽然也许你想,但我不是通灵者)。你对“复杂类型”的定义是什么?(我在维基百科上只找到了分词定义)。你是说所有的课程吗?好的,任何类都可以被赋予索引器,因此它的行为就像一个数组(),所以它实际上不是一个简单的排除。数组确实是类。在我的
IsComplexObject
方法中概述了我对复杂类的定义。除了数组类型外,该方法可以准确地识别我对复杂对象的定义。你试过了吗?这已经够疯狂了,它可能会奏效。应该能帮你做你想做的事我不知道你想要这个做什么。。。我特别想知道为什么所有的值类型都被认为不复杂,因为它们实际上可能非常复杂,并且包含许多嵌套项。我的意思是,我不是100%确定你真的想把
public struct MyStruct{public MyComplexClass Stuff{get;set;}
算作非复杂(尽管你可能想,我不是通灵者)。