如何观察数组的值类型?

时间:2018-03-15 16:10:20

标签: c# reflection

我有一个检查对象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;
}

问题是:当我有Type个简单类型数组时,例如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[]分开?

2 个答案:

答案 0 :(得分:2)

尝试检索元素类型,然后递归调用<main style="height:100%"> <a class="LC-logo" href="#/"> <i class="icon-LClogo"></i> <!-- span class="text">Luxury</span> --> </a> <div class="content"> <!-- ngView: --> <div ng-view="" id="playgroundView" class="ng-scope" style=""> <div class="row ng-scope"> <div class="container-fluid"> <div class="navbar playground-navbar"> <h3 ng-bind="board.boardName" class="ng-binding">Frank's Board</h3> </div> </div> </div> <div class="board-view-container ng-scope"> <div class="row board-view-row"> <div board="" board-data="board" edit-card-html="editCardHtml" id="board-view" style="height:100%;" class="ng-isolate-scope"> <div class="container playground-board-view"> <div class="row board-row"> <div board-lists="" lists="boardData.columns" members="boardData.team.members" class="lists-container ng-isolate-scope"> <!-- ngRepeat: list in lists --> <div ng-repeat="list in lists" class="col-sm col-md col-lg list-column ng-scope" style=""> <div class="playground-list list" ng-class="{ 'new-list' : list.newList, 'list' : !list.newList }"> <div class="container-fluid" style="padding:0;"> <div ng-click="list.title.editing = true"> <h5 ng-show="!list.title.editing" style="text-transform: capitalize;" class="list-title pull-left ng-binding"> Column 1 </h5> <input type="text" class="list-title-input ng-pristine ng-untouched ng-valid ng-not-empty ng-hide" ng-keyup="$event.keyCode === 13 ? list.title.editing = false : ''" ng-blur="list.title.editing = false" ng-model="list.data" ng-show="list.title.editing" autofocus=""> </div> </div> <hr ng-show="!list.newList"> <div class="container-fluid" style="padding:0;"> <ul dnd-list="list.items" class="list-group board-list"> <!-- ngRepeat: item in list.items --> </ul> </div> <div class="new-card" ng-show="!list.newList" ng-click="addNewItem(this.list)"> <p>Add new card...</p> </div> </div> </div> <!-- end ngRepeat: list in lists --> <div ng-repeat="list in lists" class="col-sm col-md col-lg list-column ng-scope"> <div class="playground-list list" ng-class="{ 'new-list' : list.newList, 'list' : !list.newList }"> <div class="container-fluid" style="padding:0;"> <div ng-click="list.title.editing = true"> <h5 ng-show="!list.title.editing" style="text-transform: capitalize;" class="list-title pull-left ng-binding"> Column 2 </h5> <input type="text" class="list-title-input ng-pristine ng-untouched ng-valid ng-not-empty ng-hide" ng-keyup="$event.keyCode === 13 ? list.title.editing = false : ''" ng-blur="list.title.editing = false" ng-model="list.data" ng-show="list.title.editing" autofocus=""> </div> </div> <hr ng-show="!list.newList"> <div class="container-fluid" style="padding:0;"> <ul dnd-list="list.items" class="list-group board-list"> <!-- ngRepeat: item in list.items --> </ul> </div> <div class="new-card" ng-show="!list.newList" ng-click="addNewItem(this.list)"> <p>Add new card...</p> </div> </div> </div> <div ng-repeat="list in lists" class="col-sm col-md col-lg list-column ng-scope"> <div class="playground-list list" ng-class="{ 'new-list' : list.newList, 'list' : !list.newList }"> <div class="container-fluid" style="padding:0;"> <div ng-click="list.title.editing = true"> <h5 ng-show="!list.title.editing" style="text-transform: capitalize;" class="list-title pull-left ng-binding"> Column 2 </h5> <input type="text" class="list-title-input ng-pristine ng-untouched ng-valid ng-not-empty ng-hide" ng-keyup="$event.keyCode === 13 ? list.title.editing = false : ''" ng-blur="list.title.editing = false" ng-model="list.data" ng-show="list.title.editing" autofocus=""> </div> </div> <hr ng-show="!list.newList"> <div class="container-fluid" style="padding:0;"> <ul dnd-list="list.items" class="list-group board-list"> <!-- ngRepeat: item in list.items --> </ul> </div> <div class="new-card" ng-show="!list.newList" ng-click="addNewItem(this.list)"> <p>Add new card...</p> </div> </div> </div> <div ng-repeat="list in lists" class="col-sm col-md col-lg list-column ng-scope"> <div class="playground-list list" ng-class="{ 'new-list' : list.newList, 'list' : !list.newList }"> <div class="container-fluid" style="padding:0;"> <div ng-click="list.title.editing = true"> <h5 ng-show="!list.title.editing" style="text-transform: capitalize;" class="list-title pull-left ng-binding"> Column 2 </h5> <input type="text" class="list-title-input ng-pristine ng-untouched ng-valid ng-not-empty ng-hide" ng-keyup="$event.keyCode === 13 ? list.title.editing = false : ''" ng-blur="list.title.editing = false" ng-model="list.data" ng-show="list.title.editing" autofocus=""> </div> </div> <hr ng-show="!list.newList"> <div class="container-fluid" style="padding:0;"> <ul dnd-list="list.items" class="list-group board-list"> <!-- ngRepeat: item in list.items --> </ul> </div> <div class="new-card" ng-show="!list.newList" ng-click="addNewItem(this.list)"> <p>Add new card...</p> </div> </div> </div> <div ng-repeat="list in lists" class="col-sm col-md col-lg list-column ng-scope"> <div class="playground-list list" ng-class="{ 'new-list' : list.newList, 'list' : !list.newList }"> <div class="container-fluid" style="padding:0;"> <div ng-click="list.title.editing = true"> <h5 ng-show="!list.title.editing" style="text-transform: capitalize;" class="list-title pull-left ng-binding"> Column 2 </h5> <input type="text" class="list-title-input ng-pristine ng-untouched ng-valid ng-not-empty ng-hide" ng-keyup="$event.keyCode === 13 ? list.title.editing = false : ''" ng-blur="list.title.editing = false" ng-model="list.data" ng-show="list.title.editing" autofocus=""> </div> </div> <hr ng-show="!list.newList"> <div class="container-fluid" style="padding:0;"> <ul dnd-list="list.items" class="list-group board-list"> <!-- ngRepeat: item in list.items --> </ul> </div> <div class="new-card" ng-show="!list.newList" ng-click="addNewItem(this.list)"> <p>Add new card...</p> </div> </div> </div> <div ng-repeat="list in lists" class="col-sm col-md col-lg list-column ng-scope"> <div class="playground-list list" ng-class="{ 'new-list' : list.newList, 'list' : !list.newList }"> <div class="container-fluid" style="padding:0;"> <div ng-click="list.title.editing = true"> <h5 ng-show="!list.title.editing" style="text-transform: capitalize;" class="list-title pull-left ng-binding"> Column 2 </h5> <input type="text" class="list-title-input ng-pristine ng-untouched ng-valid ng-not-empty ng-hide" ng-keyup="$event.keyCode === 13 ? list.title.editing = false : ''" ng-blur="list.title.editing = false" ng-model="list.data" ng-show="list.title.editing" autofocus=""> </div> </div> <hr ng-show="!list.newList"> <div class="container-fluid" style="padding:0;"> <ul dnd-list="list.items" class="list-group board-list"> <!-- ngRepeat: item in list.items --> </ul> </div> <div class="new-card" ng-show="!list.newList" ng-click="addNewItem(this.list)"> <p>Add new card...</p> </div> </div> </div> <div ng-repeat="list in lists" class="col-sm col-md col-lg list-column ng-scope"> <div class="playground-list list" ng-class="{ 'new-list' : list.newList, 'list' : !list.newList }"> <div class="container-fluid" style="padding:0;"> <div ng-click="list.title.editing = true"> <h5 ng-show="!list.title.editing" style="text-transform: capitalize;" class="list-title pull-left ng-binding"> Column 2 </h5> <input type="text" class="list-title-input ng-pristine ng-untouched ng-valid ng-not-empty ng-hide" ng-keyup="$event.keyCode === 13 ? list.title.editing = false : ''" ng-blur="list.title.editing = false" ng-model="list.data" ng-show="list.title.editing" autofocus=""> </div> </div> <hr ng-show="!list.newList"> <div class="container-fluid" style="padding:0;"> <ul dnd-list="list.items" class="list-group board-list"> <!-- ngRepeat: item in list.items --> </ul> </div> <div class="new-card" ng-show="!list.newList" ng-click="addNewItem(this.list)"> <p>Add new card...</p> </div> </div> </div> <div ng-repeat="list in lists" class="col-sm col-md col-lg list-column ng-scope"> <div class="playground-list list" ng-class="{ 'new-list' : list.newList, 'list' : !list.newList }"> <div class="container-fluid" style="padding:0;"> <div ng-click="list.title.editing = true"> <h5 ng-show="!list.title.editing" style="text-transform: capitalize;" class="list-title pull-left ng-binding"> Column 2 </h5> <input type="text" class="list-title-input ng-pristine ng-untouched ng-valid ng-not-empty ng-hide" ng-keyup="$event.keyCode === 13 ? list.title.editing = false : ''" ng-blur="list.title.editing = false" ng-model="list.data" ng-show="list.title.editing" autofocus=""> </div> </div> <hr ng-show="!list.newList"> <div class="container-fluid" style="padding:0;"> <ul dnd-list="list.items" class="list-group board-list"> <!-- ngRepeat: item in list.items --> </ul> </div> <div class="new-card" ng-show="!list.newList" ng-click="addNewItem(this.list)"> <p>Add new card...</p> </div> </div> </div> <div ng-repeat="list in lists" class="col-sm col-md col-lg list-column ng-scope"> <div class="playground-list list" ng-class="{ 'new-list' : list.newList, 'list' : !list.newList }"> <div class="container-fluid" style="padding:0;"> <div ng-click="list.title.editing = true"> <h5 ng-show="!list.title.editing" style="text-transform: capitalize;" class="list-title pull-left ng-binding"> Column 2 </h5> <input type="text" class="list-title-input ng-pristine ng-untouched ng-valid ng-not-empty ng-hide" ng-keyup="$event.keyCode === 13 ? list.title.editing = false : ''" ng-blur="list.title.editing = false" ng-model="list.data" ng-show="list.title.editing" autofocus=""> </div> </div> <hr ng-show="!list.newList"> <div class="container-fluid" style="padding:0;"> <ul dnd-list="list.items" class="list-group board-list"> <!-- ngRepeat: item in list.items --> </ul> </div> <div class="new-card" ng-show="!list.newList" ng-click="addNewItem(this.list)"> <p>Add new card...</p> </div> </div> </div> <!-- end ngRepeat: list in lists --> </div> <div class="col-sm col-md col-lg list-column"> <div class="playground-list new-list" ng-click="addNewList()"> <div class="container-fluid" style="padding:0;"> <h5 style="text-transform: capitalize;" class="list-title pull-left"> Add new list... <span class="glyphicon glyphicon-plus pull-right"></span> </h5> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> <footer class="pageFooter"> <div class="btnContainer"> <a class="gc-link" href="#" target="_blank"><i class="icon-handcrafted"></i></a> </div> <div class="btnContainer"> <a class="LC-logo" href="#/"> <i class="icon-LClogo"></i> </a> </div> </footer> </main>,如下所示:

IsComplexObject

对于一组&#34;复杂对象&#34;这应该返回true。 (不符合代码中规定标准的那些)。只需要注意,它也会为一个复杂对象数组或一个数组数组的数组返回true。如果这是一个问题,你可以做一个改变,这样它只会递归一次,如下所示:

if (type.IsArray) return IsComplexObject(type.GetElementType());

答案 1 :(得分:1)

也许您想检查数组排名,并重复元素类型?

import { push } from 'react-router-redux';
     API.post(`/logout`).then(
        (response) => {
         next(push(`http://www.google.com`));
    },
    (message) => {
        console.warn('logout was not successful');
    }