C# 如何访问InnerException的InnerExceptions属性?

C# 如何访问InnerException的InnerExceptions属性?,c#,.net,vb.net,exception,intuit,C#,.net,Vb.net,Exception,Intuit,在我的代码中,我使用典型的try..catch抛出并捕获了一个异常。异常中的消息是执行查询时发生错误。有关详细信息,请查看堆栈跟踪。 还有一个InnerException,抛出了消息ValidationException。没有其他InnerException。但是,当我通过Visual Studio 2015查看异常时,我可以展开异常,转到InnerException并展开它,我看到: InnerException: Nothing InnerExceptions: Count=1 然后,我可

在我的代码中,我使用典型的try..catch抛出并捕获了一个异常。异常中的消息是执行查询时发生错误。有关详细信息,请查看堆栈跟踪。 还有一个InnerException,抛出了消息
ValidationException。
没有其他InnerException。但是,当我通过Visual Studio 2015查看异常时,我可以展开异常,转到InnerException并展开它,我看到:

InnerException: Nothing
InnerExceptions: Count=1
然后,我可以展开InnerExceptions分支,查看我假设的是AggregateException,在本例中,它显示

(0): {"Error Parsing query"}
Raw View: 
然后,我可以展开(0)组以查看诸如“Detail”之类的属性,该属性提供完整详细的错误消息以及“ErrorCode”和其他许多属性

当我试图通过代码通过ex.InnerException.InnerExceptions引用“InnerExceptions”时,我不能,因为“InnerExceptions”不是“Exception”的成员

它如何在VisualStudioIDE中可见,但在代码中不可用

我正在编写使用IppDotNetSdkForQuickBooksApiV3 NuGet包的代码。我之所以提到这一点,是因为我不确定这是否是从Intuit的API中添加的。我以前从未遇到过内部异常组

需要明确的是:迭代InnerException属性不会返回与上述“Detail”中相同的错误


异常
类没有名为InnerExceptions的成员,但它是一个基类,它具有。VisualStudio的调试器将找出每个对象的类型,因此能够显示它们拥有的每个属性

但是,异常类的InnerException成员不是AggregateException类型,而是泛型异常。也就是说,VisualStudio无法确定InnerException是否会按类型实际成为AggregateException。要解决这个问题,您需要强制转换

我不太熟悉vb.net语法,在C#land中,它会这样:

((AggregateException)ex.InnerException).InnerExceptions
或者,您可以尝试这样安全地施放:

if (ex.InnerException.GetType() == typeof(AggregateException)) 
{
    var listOfInnerExceptions = ((AggregateException)ex.InnerException).InnerExceptions;
}

据我所知,VB.net中有一个
DirectCast(obj,type)
方法可用于此目的,但我可能错了。

谢谢您的快速回复。我尝试过:如果TypeOf ex.InnerException是aggregateeException,那么msg&=“很多错误!”如果TypeOf ex是aggregateeException,那么msg&=“很多错误!”如果Try'Dim aex As aggregateeException=CType(ex.InnerException,aggregateeException)Dim aex As aggregateeException=DirectCast(ex.InnerException,AggregateException)msg=aex.InnerExceptions.Count Catch ex2 As Exception msg=“仍然不起作用:”(“结束尝试,它们似乎都不起作用。抱歉,我似乎无法在注释中使用代码格式。如果有帮助,监视窗口将第一个InnerException显示为System.Exception{Intuit.Ipp.Exception.ValidationException}和InnerException的类型为System.Collections.ObjectModel.ReadOnlyCollection(Intuit.Ipp.Exception.IdsError的)啊!我看到您使用的库不是.Net的一部分…这让事情有点不同:您必须将InnerException强制转换为Intuit.Ipp.Exception.ValidationException,而不是AggregateException(这是一个默认的.Net对象,碰巧具有相同类型的成员,我错误地认为您正在处理这一点…)重点是你应该告诉VS在哪里寻找InnerExceptions成员。