繁体   English   中英

检查对象中是否有任何空值

[英]Check for any null values in an object

如果我有一个具有其他嵌套对象和属性的对象,例如下面的对象和属性。

var request = new GetInfoRequest
{
   GetInformation = new GetInformationType
   {
      Code = "abc",
      Id = "123",
      Item = new InfoItem
      {
         Itemid = "test",
         ItemName = "testname"
      },
      StartDate = new StartdatumType { Start = new DateTime(1990, 1, 1)},
      EndDate = new EndDateType { End = new DateTime.Now }    
   }
};

当将此对象传递给函数时,我要检查其属性或对象均不为null

public InfoResponse getInfo(request)
{
  // Check that the request object has no null properties or objects.
}

有没有比使用if语句单步检查每个子对象和属性更简单的方法了? 递归方法还是类似的方法?

延伸
在我的getInfo函数中,我不需要这样写:

if (request != null && request.GetInformation != null && ... etc.)

使用反射并遍历所有属性以检查是否为空。 这是一个入门片段

using System.Reflection;

GetInfoRequest objGetInfoRequest;
Type getInfoRequestType = objGetInfoRequest.GetType();
PropertyInfo[] myProps = getInfoRequestType.GetProperties();

您可以使用反射来遍历字段。

您非常需要对嵌套值进行一些递归。

检查 IEnumerable<object> 里面有任何值而没有得到 IndexOutOfRangeException<div id="text_translate"><p> 我有一个IEnumerable&lt;object&gt; 。 我想检查IEnumerable&lt;object&gt;里面是否有任何值。</p><p> 我的方法如下所示:</p><pre> private static JObject TransformExcelDatasource(Stream originalDatasource) { // Read Excel Data var excelReader = new ExcelReader(originalDatasource); var sheetReader = excelReader[0]; //Check if row has any values var row = sheetReader.Row(100); var rowHasValues = row.Any(); if (rowHasValues) { //do stuff } }</pre><p> 我尝试获取只有 3 行的 excel 表的第 100 行。 我知道,这只是一个 POC。</p><p> 行var rowHasValues = row.Any(); throws System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'</p><p> 这是变量IEnumerable&lt;object&gt; row的样子: <a href="https://i.stack.imgur.com/quYVJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/quYVJ.png" alt="在此处输入图像描述"></a></p><p> 我尝试了这个问题的答案( <a href="https://stackoverflow.com/questions/52575358/ienumerable-indexoutofrangeexception" rel="nofollow noreferrer">第一个</a>, <a href="https://stackoverflow.com/questions/27277534/index-out-of-bounds-in-linq-enumerable" rel="nofollow noreferrer">第二个</a>),但对我没有任何帮助。</p><p> <strong>如何检查IEnumerable&lt;object&gt; row是否有任何值而不获得 IndexOutOfRangeException?</strong></p><p> <strong>更新</strong></p><p>这是抛出的 IndexOutOfRangeException <a href="https://i.stack.imgur.com/SJN18.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SJN18.png" alt="![在此处输入图像描述"></a></p><p> row.Current也不是一个选项,因为它IEnumerable&lt;object&gt;不包含它。 <a href="https://i.stack.imgur.com/GshWk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GshWk.png" alt="在此处输入图像描述"></a></p><p> 完整的堆栈跟踪:</p><pre> at LightWeightExcelReader.SheetReader.get_Item(String cellAddress) at LightWeightExcelReader.SheetReader.&lt;get_Item&gt;b__8_1(String x) at System.Linq.Utilities.&lt;&gt;c__DisplayClass2_0`3.&lt;CombineSelectors&gt;b__0(TSource x) at System.Linq.Enumerable.SelectListIterator`2.MoveNext() at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source) at NR.DataAdapter.Core.Services.TransformDatasourceService.TransformExcelDatasource(Stream originalDatasource) in C:\Dev\src\ipa-dataadapter\NR.DataAdapter.Core\Services\TransformDatasourceService.cs:line 35 at NR.DataAdapter.Core.Services.TransformDatasourceService.TransformDatasource(IDatasource interpretedDatasource) in C:\Dev\src\ipa-dataadapter\NR.DataAdapter.Core\Services\TransformDatasourceService.cs:line 19 at NR.DataAdapter.Core.DataAdapter.AdaptDatasources() in C:\Dev\src\ipa-dataadapter\NR.DataAdapter.Core\DataAdapter.cs:line 24 at NR.DataAdapter.UI.Controllers.HomeController.Dashboard() in C:\Dev\src\ipa-dataadapter\NR.DataAdapter.UI\Controllers\HomeController.cs:line 24 at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()</pre></div></object>

[英]Check if IEnumerable<object> has any values inside without getting IndexOutOfRangeException

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查对象的空值 检查字典 c# 中可用的任何非 null 值 如何在进一步处理之前检查列表中的任何空值? 检查对象的任何属性是否为null或为空的最佳方法是什么? 检查对象中的任何一个是否为空并继续处理其他对象 检查任何属性为空 检查参数值是否为null,然后初始化对象。 这是个好主意吗? 检查 IEnumerable<object> 里面有任何值而没有得到 IndexOutOfRangeException<div id="text_translate"><p> 我有一个IEnumerable&lt;object&gt; 。 我想检查IEnumerable&lt;object&gt;里面是否有任何值。</p><p> 我的方法如下所示:</p><pre> private static JObject TransformExcelDatasource(Stream originalDatasource) { // Read Excel Data var excelReader = new ExcelReader(originalDatasource); var sheetReader = excelReader[0]; //Check if row has any values var row = sheetReader.Row(100); var rowHasValues = row.Any(); if (rowHasValues) { //do stuff } }</pre><p> 我尝试获取只有 3 行的 excel 表的第 100 行。 我知道,这只是一个 POC。</p><p> 行var rowHasValues = row.Any(); throws System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'</p><p> 这是变量IEnumerable&lt;object&gt; row的样子: <a href="https://i.stack.imgur.com/quYVJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/quYVJ.png" alt="在此处输入图像描述"></a></p><p> 我尝试了这个问题的答案( <a href="https://stackoverflow.com/questions/52575358/ienumerable-indexoutofrangeexception" rel="nofollow noreferrer">第一个</a>, <a href="https://stackoverflow.com/questions/27277534/index-out-of-bounds-in-linq-enumerable" rel="nofollow noreferrer">第二个</a>),但对我没有任何帮助。</p><p> <strong>如何检查IEnumerable&lt;object&gt; row是否有任何值而不获得 IndexOutOfRangeException?</strong></p><p> <strong>更新</strong></p><p>这是抛出的 IndexOutOfRangeException <a href="https://i.stack.imgur.com/SJN18.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SJN18.png" alt="![在此处输入图像描述"></a></p><p> row.Current也不是一个选项,因为它IEnumerable&lt;object&gt;不包含它。 <a href="https://i.stack.imgur.com/GshWk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GshWk.png" alt="在此处输入图像描述"></a></p><p> 完整的堆栈跟踪:</p><pre> at LightWeightExcelReader.SheetReader.get_Item(String cellAddress) at LightWeightExcelReader.SheetReader.&lt;get_Item&gt;b__8_1(String x) at System.Linq.Utilities.&lt;&gt;c__DisplayClass2_0`3.&lt;CombineSelectors&gt;b__0(TSource x) at System.Linq.Enumerable.SelectListIterator`2.MoveNext() at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source) at NR.DataAdapter.Core.Services.TransformDatasourceService.TransformExcelDatasource(Stream originalDatasource) in C:\Dev\src\ipa-dataadapter\NR.DataAdapter.Core\Services\TransformDatasourceService.cs:line 35 at NR.DataAdapter.Core.Services.TransformDatasourceService.TransformDatasource(IDatasource interpretedDatasource) in C:\Dev\src\ipa-dataadapter\NR.DataAdapter.Core\Services\TransformDatasourceService.cs:line 19 at NR.DataAdapter.Core.DataAdapter.AdaptDatasources() in C:\Dev\src\ipa-dataadapter\NR.DataAdapter.Core\DataAdapter.cs:line 24 at NR.DataAdapter.UI.Controllers.HomeController.Dashboard() in C:\Dev\src\ipa-dataadapter\NR.DataAdapter.UI\Controllers\HomeController.cs:line 24 at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()</pre></div></object> 如何检查空值? 检查数据集是否为空值
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM