Skip to content

Conversation

@daxian-dbw
Copy link
Member

@daxian-dbw daxian-dbw commented Mar 13, 2020

PR Summary

Fix #11692

Check for null value argument when assigning to the Enum type ActionPreference for common parameters, just like what we do for non-optimized setters from the below code:

Expression expr = Expression.Assign(propertyExpr, Expression.Convert(value, propertyExpr.Type));
if (propertyExpr.Type.IsValueType && Nullable.GetUnderlyingType(propertyExpr.Type) == null)
{
var throwInvalidCastExceptionExpr =
Expression.Call(Language.CachedReflectionInfo.LanguagePrimitives_ThrowInvalidCastException,
Language.ExpressionCache.NullConstant,
Expression.Constant(propertyExpr.Type, typeof(Type)));
// The return type of 'ThrowInvalidCastException' is System.Object, but the method actually always
// throws 'PSInvalidCastException' when it's executed. So converting 'throwInvalidCastExceptionExpr'
// to 'propertyExpr.Type' is fine, because the conversion will never be hit.
expr = Expression.Condition(Expression.Equal(value, Language.ExpressionCache.NullConstant),
Expression.Convert(throwInvalidCastExceptionExpr, propertyExpr.Type),
expr);
}
return Expression.Lambda<Action<object, object>>(expr, new[] { target, value }).Compile();

PR Checklist

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Mar 16, 2020
@iSazonov iSazonov merged commit c737f31 into PowerShell:master Mar 18, 2020
@daxian-dbw daxian-dbw deleted the binder branch March 18, 2020 23:22
@ghost
Copy link

ghost commented Mar 26, 2020

🎉v7.1.0-preview.1 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ErrorAction for Test-Path throw an NullReferenceException

3 participants