From 72835992ec42f201ffa643313232e68ec275d9be Mon Sep 17 00:00:00 2001 From: Christoph Bergmeister Date: Wed, 9 May 2018 22:26:10 +0100 Subject: [PATCH] catch specific error for custom attribute being defined in other file --- Rules/UseShouldProcessCorrectly.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Rules/UseShouldProcessCorrectly.cs b/Rules/UseShouldProcessCorrectly.cs index 6be9db87e..1ddd14696 100644 --- a/Rules/UseShouldProcessCorrectly.cs +++ b/Rules/UseShouldProcessCorrectly.cs @@ -312,17 +312,27 @@ private bool SupportsShouldProcess(string cmdName) var funcInfo = cmdInfo as FunctionInfo; if (funcInfo != null && funcInfo.CmdletBinding - && funcInfo.ScriptBlock != null - && funcInfo.ScriptBlock.Attributes != null) + && funcInfo.ScriptBlock != null) { - foreach (var attr in funcInfo.ScriptBlock.Attributes) + try { - var cmdletBindingAttr = attr as CmdletBindingAttribute; - if (cmdletBindingAttr != null) + if (funcInfo.ScriptBlock.Attributes != null) { - return cmdletBindingAttr.SupportsShouldProcess; + foreach (var attr in funcInfo.ScriptBlock.Attributes) + { + var cmdletBindingAttr = attr as CmdletBindingAttribute; + if (cmdletBindingAttr != null) + { + return cmdletBindingAttr.SupportsShouldProcess; + } + } } } + catch (RuntimeException) + { + // The call to .Attributes on funcInfo.ScriptBlock.Attributes can throw if the attribute is custom and defined in a different file, therefore no action is being taken in this case + } + } return false;