Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions 19 src/System.Management.Automation/engine/LanguagePrimitives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,11 @@ public static class LanguagePrimitives

internal static void CreateMemberNotFoundError(PSObject pso, DictionaryEntry property, Type resultType)
{
string availableProperties = GetAvailableProperties(pso);
string settableProperties = GetSettableProperties(pso);

string message = StringUtil.Format(ExtendedTypeSystem.PropertyNotFound, property.Key.ToString(), resultType.FullName, availableProperties);
string message = settableProperties == string.Empty
? StringUtil.Format(ExtendedTypeSystem.NoSettableProperty, property.Key.ToString(), resultType.FullName)
: StringUtil.Format(ExtendedTypeSystem.PropertyNotFound, property.Key.ToString(), resultType.FullName, settableProperties);

typeConversion.WriteLine("Issuing an error message about not being able to create an object from hashtable.");
throw new InvalidOperationException(message);
Expand Down Expand Up @@ -4735,18 +4737,23 @@ internal static PSObject SetObjectProperties(object o, IDictionary properties, T
return pso;
}

private static string GetAvailableProperties(PSObject pso)
private static string GetSettableProperties(PSObject pso)
{
if (pso is null || pso.Properties is null)
{
return string.Empty;
}

StringBuilder availableProperties = new StringBuilder();
bool first = true;

if (pso != null && pso.Properties != null)
foreach (PSPropertyInfo p in pso.Properties)
{
foreach (PSPropertyInfo p in pso.Properties)
if (p.IsSettable)
{
if (!first)
{
availableProperties.Append(" , ");
availableProperties.Append(", ");
}

availableProperties.Append("[" + p.Name + " <" + p.TypeNameOfValue + ">]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,10 @@
<value>"{0}" returned a null value.</value>
</data>
<data name="PropertyNotFound" xml:space="preserve">
<value>The {0} property was not found for the {1} object. The available property is: {2}</value>
<value>The property '{0}' was not found for the '{1}' object. The settable properties are: {2}.</value>
</data>
<data name="NoSettableProperty" xml:space="preserve">
<value>The property '{0}' was not found for the '{1}' object. There is no settable property available.</value>
</data>
<data name="ObjectCreationError" xml:space="preserve">
<value>Cannot create object of type "{0}". {1}</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,44 @@ Describe "Tests for hashtable to PSCustomObject conversion" -Tags "CI" {
}
}

Describe "Error message with settable Property information" {
BeforeAll {
Add-Type @"
namespace HashtableConversionTest {
public class AType {
public string Name;
public string Path { get; set; }
public string Id { get; }
}
}
"@
}

It "Only settable properties are called out in the error message" {
daxian-dbw marked this conversation as resolved.
Outdated
Show resolved Hide resolved
try {
[HashtableConversionTest.AType]@{ key = 1 }
} catch {
$e = $_
}

$e.FullyQualifiedErrorId | Should -BeExactly "ObjectCreationError"
$e.Exception.Message.Contains("key") | Should -BeTrue
$e.Exception.Message.Contains("Name") | Should -BeTrue
daxian-dbw marked this conversation as resolved.
Outdated
Show resolved Hide resolved
$e.Exception.Message.Contains("Path") | Should -BeTrue
$e.Exception.Message.Contains("Id") | Should -BeFalse
}

It "Shows no property when there is no settable property" {
try {
[System.Collections.Specialized.OrderedDictionary]@{ key = 1 }
} catch {
$e = $_
}

$type = [psobject].Assembly.GetType("ExtendedTypeSystem")
$property = $type.GetProperty("NoSettableProperty", @("NonPublic", "Static"))
$resString = $property.GetValue($null) -f 'key', 'System.Collections.Specialized.OrderedDictionary'

$e.Exception.Message | Should -BeLike "*$resString"
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.