diff --git a/src/System.Management.Automation/engine/CoreAdapter.cs b/src/System.Management.Automation/engine/CoreAdapter.cs index 119557dd79c..442da89fd3a 100644 --- a/src/System.Management.Automation/engine/CoreAdapter.cs +++ b/src/System.Management.Automation/engine/CoreAdapter.cs @@ -354,14 +354,16 @@ private static Exception NewException( { Exception inner = ex.InnerException ?? ex; newParameters[0] = inner.Message; - return new ExtendedTypeSystemException(targetErrorId, + return new ExtendedTypeSystemException( + targetErrorId, inner, resourceString, newParameters); } newParameters[0] = e.Message; - return new ExtendedTypeSystemException(errorId, + return new ExtendedTypeSystemException( + errorId, e, resourceString, newParameters); @@ -380,8 +382,11 @@ internal ConsolidatedString BaseGetTypeNameHierarchy(object obj) catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBaseGetTypeNameHierarchy", "CatchFromBaseGetTypeNameHierarchyTI", - ExtendedTypeSystem.ExceptionRetrievingTypeNameHierarchy); + throw NewException( + e, + "CatchFromBaseGetTypeNameHierarchy", + "CatchFromBaseGetTypeNameHierarchyTI", + ExtendedTypeSystem.ExceptionRetrievingTypeNameHierarchy); } } @@ -394,8 +399,12 @@ internal T BaseGetMember(object obj, string memberName) where T : PSMemberInf catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBaseGetMember", "CatchFromBaseGetMemberTI", - ExtendedTypeSystem.ExceptionGettingMember, memberName); + throw NewException( + e, + "CatchFromBaseGetMember", + "CatchFromBaseGetMemberTI", + ExtendedTypeSystem.ExceptionGettingMember, + memberName); } } @@ -408,8 +417,12 @@ internal T BaseGetFirstMemberOrDefault(object obj, MemberNamePredicate predic catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBaseGetMember", "CatchFromBaseGetMemberTI", - ExtendedTypeSystem.ExceptionGettingMember, nameof(predicate)); + throw NewException( + e, + "CatchFromBaseGetMember", + "CatchFromBaseGetMemberTI", + ExtendedTypeSystem.ExceptionGettingMember, + nameof(predicate)); } } @@ -422,7 +435,10 @@ internal PSMemberInfoInternalCollection BaseGetMembers(object obj) where T catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBaseGetMembers", "CatchFromBaseGetMembersTI", + throw NewException( + e, + "CatchFromBaseGetMembers", + "CatchFromBaseGetMembersTI", ExtendedTypeSystem.ExceptionGettingMembers); } } @@ -440,7 +456,8 @@ internal object BasePropertyGet(PSProperty property) catch (TargetInvocationException ex) { Exception inner = ex.InnerException ?? ex; - throw new GetValueInvocationException("CatchFromBaseAdapterGetValueTI", + throw new GetValueInvocationException( + "CatchFromBaseAdapterGetValueTI", inner, ExtendedTypeSystem.ExceptionWhenGetting, property.Name, inner.Message); @@ -448,7 +465,8 @@ internal object BasePropertyGet(PSProperty property) catch (GetValueException) { throw; } catch (Exception e) { - throw new GetValueInvocationException("CatchFromBaseAdapterGetValue", + throw new GetValueInvocationException( + "CatchFromBaseAdapterGetValue", e, ExtendedTypeSystem.ExceptionWhenGetting, property.Name, e.Message); @@ -464,7 +482,8 @@ internal void BasePropertySet(PSProperty property, object setValue, bool convert catch (TargetInvocationException ex) { Exception inner = ex.InnerException ?? ex; - throw new SetValueInvocationException("CatchFromBaseAdapterSetValueTI", + throw new SetValueInvocationException( + "CatchFromBaseAdapterSetValueTI", inner, ExtendedTypeSystem.ExceptionWhenSetting, property.Name, inner.Message); @@ -472,7 +491,8 @@ internal void BasePropertySet(PSProperty property, object setValue, bool convert catch (SetValueException) { throw; } catch (Exception e) { - throw new SetValueInvocationException("CatchFromBaseAdapterSetValue", + throw new SetValueInvocationException( + "CatchFromBaseAdapterSetValue", e, ExtendedTypeSystem.ExceptionWhenSetting, property.Name, e.Message); @@ -488,8 +508,12 @@ internal bool BasePropertyIsSettable(PSProperty property) catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBasePropertyIsSettable", "CatchFromBasePropertyIsSettableTI", - ExtendedTypeSystem.ExceptionRetrievingPropertyWriteState, property.Name); + throw NewException( + e, + "CatchFromBasePropertyIsSettable", + "CatchFromBasePropertyIsSettableTI", + ExtendedTypeSystem.ExceptionRetrievingPropertyWriteState, + property.Name); } } @@ -502,8 +526,12 @@ internal bool BasePropertyIsGettable(PSProperty property) catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBasePropertyIsGettable", "CatchFromBasePropertyIsGettableTI", - ExtendedTypeSystem.ExceptionRetrievingPropertyReadState, property.Name); + throw NewException( + e, + "CatchFromBasePropertyIsGettable", + "CatchFromBasePropertyIsGettableTI", + ExtendedTypeSystem.ExceptionRetrievingPropertyReadState, + property.Name); } } @@ -516,8 +544,12 @@ internal string BasePropertyType(PSProperty property) catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBasePropertyType", "CatchFromBasePropertyTypeTI", - ExtendedTypeSystem.ExceptionRetrievingPropertyType, property.Name); + throw NewException( + e, + "CatchFromBasePropertyType", + "CatchFromBasePropertyTypeTI", + ExtendedTypeSystem.ExceptionRetrievingPropertyType, + property.Name); } } @@ -530,8 +562,12 @@ internal string BasePropertyToString(PSProperty property) catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBasePropertyToString", "CatchFromBasePropertyToStringTI", - ExtendedTypeSystem.ExceptionRetrievingPropertyString, property.Name); + throw NewException( + e, + "CatchFromBasePropertyToString", + "CatchFromBasePropertyToStringTI", + ExtendedTypeSystem.ExceptionRetrievingPropertyString, + property.Name); } } @@ -544,8 +580,12 @@ internal AttributeCollection BasePropertyAttributes(PSProperty property) catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBasePropertyAttributes", "CatchFromBasePropertyAttributesTI", - ExtendedTypeSystem.ExceptionRetrievingPropertyAttributes, property.Name); + throw NewException( + e, + "CatchFromBasePropertyAttributes", + "CatchFromBasePropertyAttributesTI", + ExtendedTypeSystem.ExceptionRetrievingPropertyAttributes, + property.Name); } } @@ -561,10 +601,13 @@ internal object BaseMethodInvoke(PSMethod method, PSMethodInvocationConstraints catch (TargetInvocationException ex) { Exception inner = ex.InnerException ?? ex; - throw new MethodInvocationException("CatchFromBaseAdapterMethodInvokeTI", + throw new MethodInvocationException( + "CatchFromBaseAdapterMethodInvokeTI", inner, ExtendedTypeSystem.MethodInvocationException, - method.Name, arguments.Length, inner.Message); + method.Name, + arguments.Length, + inner.Message); } catch (FlowControlException) { throw; } catch (ScriptCallDepthException) { throw; } @@ -580,10 +623,13 @@ internal object BaseMethodInvoke(PSMethod method, PSMethodInvocationConstraints throw; } - throw new MethodInvocationException("CatchFromBaseAdapterMethodInvoke", + throw new MethodInvocationException( + "CatchFromBaseAdapterMethodInvoke", e, ExtendedTypeSystem.MethodInvocationException, - method.Name, arguments.Length, e.Message); + method.Name, + arguments.Length, + e.Message); } } @@ -596,8 +642,12 @@ internal Collection BaseMethodDefinitions(PSMethod method) catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBaseMethodDefinitions", "CatchFromBaseMethodDefinitionsTI", - ExtendedTypeSystem.ExceptionRetrievingMethodDefinitions, method.Name); + throw NewException( + e, + "CatchFromBaseMethodDefinitions", + "CatchFromBaseMethodDefinitionsTI", + ExtendedTypeSystem.ExceptionRetrievingMethodDefinitions, + method.Name); } } @@ -610,8 +660,12 @@ internal string BaseMethodToString(PSMethod method) catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBaseMethodToString", "CatchFromBaseMethodToStringTI", - ExtendedTypeSystem.ExceptionRetrievingMethodString, method.Name); + throw NewException( + e, + "CatchFromBaseMethodToString", + "CatchFromBaseMethodToStringTI", + ExtendedTypeSystem.ExceptionRetrievingMethodString, + method.Name); } } #endregion method @@ -626,8 +680,12 @@ internal string BaseParameterizedPropertyType(PSParameterizedProperty property) catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBaseParameterizedPropertyType", "CatchFromBaseParameterizedPropertyTypeTI", - ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertytype, property.Name); + throw NewException( + e, + "CatchFromBaseParameterizedPropertyType", + "CatchFromBaseParameterizedPropertyTypeTI", + ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertytype, + property.Name); } } @@ -640,8 +698,12 @@ internal bool BaseParameterizedPropertyIsSettable(PSParameterizedProperty proper catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBaseParameterizedPropertyIsSettable", "CatchFromBaseParameterizedPropertyIsSettableTI", - ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertyWriteState, property.Name); + throw NewException( + e, + "CatchFromBaseParameterizedPropertyIsSettable", + "CatchFromBaseParameterizedPropertyIsSettableTI", + ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertyWriteState, + property.Name); } } @@ -654,8 +716,12 @@ internal bool BaseParameterizedPropertyIsGettable(PSParameterizedProperty proper catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBaseParameterizedPropertyIsGettable", "CatchFromBaseParameterizedPropertyIsGettableTI", - ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertyReadState, property.Name); + throw NewException( + e, + "CatchFromBaseParameterizedPropertyIsGettable", + "CatchFromBaseParameterizedPropertyIsGettableTI", + ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertyReadState, + property.Name); } } @@ -668,8 +734,12 @@ internal Collection BaseParameterizedPropertyDefinitions(PSParameterized catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBaseParameterizedPropertyDefinitions", "CatchFromBaseParameterizedPropertyDefinitionsTI", - ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertyDefinitions, property.Name); + throw NewException( + e, + "CatchFromBaseParameterizedPropertyDefinitions", + "CatchFromBaseParameterizedPropertyDefinitionsTI", + ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertyDefinitions, + property.Name); } } @@ -682,18 +752,22 @@ internal object BaseParameterizedPropertyGet(PSParameterizedProperty property, p catch (TargetInvocationException ex) { Exception inner = ex.InnerException ?? ex; - throw new GetValueInvocationException("CatchFromBaseAdapterParameterizedPropertyGetValueTI", + throw new GetValueInvocationException( + "CatchFromBaseAdapterParameterizedPropertyGetValueTI", inner, ExtendedTypeSystem.ExceptionWhenGetting, - property.Name, inner.Message); + property.Name, + inner.Message); } catch (GetValueException) { throw; } catch (Exception e) { - throw new GetValueInvocationException("CatchFromBaseParameterizedPropertyAdapterGetValue", + throw new GetValueInvocationException( + "CatchFromBaseParameterizedPropertyAdapterGetValue", e, ExtendedTypeSystem.ExceptionWhenGetting, - property.Name, e.Message); + property.Name, + e.Message); } } @@ -706,18 +780,22 @@ internal void BaseParameterizedPropertySet(PSParameterizedProperty property, obj catch (TargetInvocationException ex) { Exception inner = ex.InnerException ?? ex; - throw new SetValueInvocationException("CatchFromBaseAdapterParameterizedPropertySetValueTI", + throw new SetValueInvocationException( + "CatchFromBaseAdapterParameterizedPropertySetValueTI", inner, ExtendedTypeSystem.ExceptionWhenSetting, - property.Name, inner.Message); + property.Name, + inner.Message); } catch (SetValueException) { throw; } catch (Exception e) { - throw new SetValueInvocationException("CatchFromBaseAdapterParameterizedPropertySetValue", + throw new SetValueInvocationException( + "CatchFromBaseAdapterParameterizedPropertySetValue", e, ExtendedTypeSystem.ExceptionWhenSetting, - property.Name, e.Message); + property.Name, + e.Message); } } @@ -730,8 +808,12 @@ internal string BaseParameterizedPropertyToString(PSParameterizedProperty proper catch (ExtendedTypeSystemException) { throw; } catch (Exception e) { - throw NewException(e, "CatchFromBaseParameterizedPropertyToString", "CatchFromBaseParameterizedPropertyToStringTI", - ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertyString, property.Name); + throw NewException( + e, + "CatchFromBaseParameterizedPropertyToString", + "CatchFromBaseParameterizedPropertyToStringTI", + ExtendedTypeSystem.ExceptionRetrievingParameterizedPropertyString, + property.Name); } } diff --git a/src/System.Management.Automation/engine/MshObject.cs b/src/System.Management.Automation/engine/MshObject.cs index edf41c7e34c..7b88718e155 100644 --- a/src/System.Management.Automation/engine/MshObject.cs +++ b/src/System.Management.Automation/engine/MshObject.cs @@ -2496,7 +2496,9 @@ private static void AddGenericArguments(StringBuilder sb, Type[] genericArgument internal static string Type(Type type, bool dropNamespaces = false, string key = null) { if (type == null) + { return string.Empty; + } string result; if (type.IsGenericType && !type.IsGenericTypeDefinition) diff --git a/test/xUnit/csharp/test_PSObject.cs b/test/xUnit/csharp/test_PSObject.cs index 46497065fd1..5f4c6d41869 100644 --- a/test/xUnit/csharp/test_PSObject.cs +++ b/test/xUnit/csharp/test_PSObject.cs @@ -19,16 +19,16 @@ public static class PSObjectTests [Fact] public static void TestEmptyObjectHasNoProperty() { - var psObject = new PSObject(); - var actual = psObject.GetFirstPropertyOrDefault(name => true); + var pso = new PSObject(); + var actual = pso.GetFirstPropertyOrDefault(name => true); Assert.Null(actual); } [Fact] public static void TestWrappedDateTimeHasReflectedMember() { - var psObject = new PSObject(DateTime.Now); - var member = psObject.GetFirstPropertyOrDefault(name => name == "DayOfWeek"); + var pso = new PSObject(DateTime.Now); + var member = pso.GetFirstPropertyOrDefault(name => name == "DayOfWeek"); Assert.NotNull(member); Assert.Equal("DayOfWeek", member.Name); } @@ -36,9 +36,9 @@ public static void TestWrappedDateTimeHasReflectedMember() [Fact] public static void TestAdaptedMember() { - var psObject = new PSObject(DateTime.Now); - psObject.Members.Add(new PSNoteProperty("NewMember", "AValue")); - var member = psObject.GetFirstPropertyOrDefault(name => name == "NewMember"); + var pso = new PSObject(DateTime.Now); + pso.Members.Add(new PSNoteProperty("NewMember", "AValue")); + var member = pso.GetFirstPropertyOrDefault(name => name == "NewMember"); Assert.NotNull(member); Assert.Equal("NewMember", member.Name); } @@ -46,9 +46,9 @@ public static void TestAdaptedMember() [Fact] public static void TestShadowedMember() { - var psObject = new PSObject(DateTime.Now); - psObject.Members.Add(new PSNoteProperty("DayOfWeek", "AValue")); - var member = psObject.GetFirstPropertyOrDefault(name => name == "DayOfWeek"); + var pso = new PSObject(DateTime.Now); + pso.Members.Add(new PSNoteProperty("DayOfWeek", "AValue")); + var member = pso.GetFirstPropertyOrDefault(name => name == "DayOfWeek"); Assert.NotNull(member); Assert.Equal("DayOfWeek", member.Name); Assert.Equal("AValue", member.Value); @@ -57,24 +57,24 @@ public static void TestShadowedMember() [Fact] public static void TestMemberSetIsNotProperty() { - var psObject = new PSObject(DateTime.Now); + var pso = new PSObject(DateTime.Now); var psNoteProperty = new PSNoteProperty("NewMember", "AValue"); - psObject.Members.Add(psNoteProperty); - psObject.Members.Add(new PSMemberSet("NewMemberSet", new[] { psNoteProperty })); + pso.Members.Add(psNoteProperty); + pso.Members.Add(new PSMemberSet("NewMemberSet", new[] { psNoteProperty })); - var member = psObject.GetFirstPropertyOrDefault(name => name == "NewMemberSet"); + var member = pso.GetFirstPropertyOrDefault(name => name == "NewMemberSet"); Assert.Null(member); } [Fact] public static void TestMemberSet() { - var psObject = new PSObject(DateTime.Now); + var pso = new PSObject(DateTime.Now); var psNoteProperty = new PSNoteProperty("NewMember", "AValue"); - psObject.Members.Add(psNoteProperty); - psObject.Members.Add(new PSMemberSet("NewMemberSet", new[] { psNoteProperty })); + pso.Members.Add(psNoteProperty); + pso.Members.Add(new PSMemberSet("NewMemberSet", new[] { psNoteProperty })); - var member = psObject.Members.FirstOrDefault(name => name == "NewMemberSet"); + var member = pso.Members.FirstOrDefault(name => name == "NewMemberSet"); Assert.NotNull(member); Assert.Equal("NewMemberSet", member.Name); } @@ -89,8 +89,8 @@ public static void TextXmlElementMember() root.AppendChild(firstChild); root.InsertAfter(doc.CreateElement("elem2"), firstChild); - var psObject = new PSObject(root); - var member = psObject.GetFirstPropertyOrDefault(name => name.StartsWith("elem")); + var pso = new PSObject(root); + var member = pso.GetFirstPropertyOrDefault(name => name.StartsWith("elem")); Assert.Equal("elem1", member.Name); } @@ -103,8 +103,8 @@ public static void TextXmlAttributeMember() root.SetAttribute("attr", "value"); root.AppendChild(doc.CreateElement("elem")); - var psObject = new PSObject(root); - var member = psObject.GetFirstPropertyOrDefault(name => name.StartsWith("attr")); + var pso = new PSObject(root); + var member = pso.GetFirstPropertyOrDefault(name => name.StartsWith("attr")); Assert.Equal("attr", member.Name); }