Skip to content

Commit 6ede78d

Browse files
committed
Merge pull request #1796 from sharwell/fix-1795
Unwrap the TargetInvocationException for Reflection calls
2 parents 85ab2fc + 0bb2d99 commit 6ede78d

1 file changed

Lines changed: 20 additions & 3 deletions

File tree

StyleCop.Analyzers/StyleCop.Analyzers/Settings/SettingsHelper.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace StyleCop.Analyzers
99
using System.IO;
1010
using System.Linq;
1111
using System.Reflection;
12+
using System.Runtime.ExceptionServices;
1213
using System.Threading;
1314
using Microsoft.CodeAnalysis;
1415
using Microsoft.CodeAnalysis.Diagnostics;
@@ -134,8 +135,16 @@ private static object GetField(object obj, string name)
134135

135136
private static object CallMethod(object obj, string name, Type[] parameters, params object[] arguments)
136137
{
137-
MethodInfo methodInfo = obj?.GetType().GetRuntimeMethod(name, parameters);
138-
return methodInfo?.Invoke(obj, arguments);
138+
try
139+
{
140+
MethodInfo methodInfo = obj?.GetType().GetRuntimeMethod(name, parameters);
141+
return methodInfo?.Invoke(obj, arguments);
142+
}
143+
catch (TargetInvocationException ex)
144+
{
145+
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
146+
throw;
147+
}
139148
}
140149

141150
private static object GetProperty(object obj, string name)
@@ -152,7 +161,15 @@ private static object GetProperty(object obj, string name)
152161
propertyInfo = propertiesForType.GetOrAdd(name, _ => obj.GetType().GetRuntimeProperty(name));
153162
}
154163

155-
return propertyInfo?.GetValue(obj);
164+
try
165+
{
166+
return propertyInfo?.GetValue(obj);
167+
}
168+
catch (TargetInvocationException ex)
169+
{
170+
ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
171+
throw;
172+
}
156173
}
157174
}
158175
}

0 commit comments

Comments
 (0)