diff options
author | Marek Safar <marek.safar@gmail.com> | 2016-09-02 12:01:17 +0300 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2016-09-02 12:02:52 +0300 |
commit | ceb298c91e9277124660ac45070d4697d3ad6ede (patch) | |
tree | 6b4021793ee5d1e4ae197e58f01047b56915fe0f /mcs/class/dlr | |
parent | 1a337a898cb59d1de5101f75ba1aa59be5ac4b45 (diff) |
[System.Core] Allow TryConvert to return null for any reference types (not only object). Fixes #41509
Diffstat (limited to 'mcs/class/dlr')
-rw-r--r-- | mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/DynamicObject.cs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/DynamicObject.cs b/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/DynamicObject.cs index 71341f5bc2d..7da7303d755 100644 --- a/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/DynamicObject.cs +++ b/mcs/class/dlr/Runtime/Microsoft.Scripting.Core/Actions/DynamicObject.cs @@ -501,6 +501,44 @@ namespace System.Dynamic { binder.ReturnType ); +#if MONO // referencesource version + Expression condition; + // If the return type can not be assigned null then just check for type assignablity otherwise allow null. + if (binder.ReturnType.IsValueType && Nullable.GetUnderlyingType(binder.ReturnType) == null) { + condition = Expression.TypeIs(resultMO.Expression, binder.ReturnType); + } + else { + condition = Expression.OrElse( + Expression.Equal(resultMO.Expression, Expression.Constant(null)), + Expression.TypeIs(resultMO.Expression, binder.ReturnType)); + } + + var checkedConvert = Expression.Condition( + condition, + convert, + Expression.Throw( + Expression.New(typeof(InvalidCastException).GetConstructor(new Type[]{typeof(string)}), + Expression.Call( + typeof(string).GetMethod("Format", new Type[] {typeof(string), typeof(object[])}), + Expression.Constant(convertFailed), + Expression.NewArrayInit(typeof(object), + Expression.Condition( + Expression.Equal(resultMO.Expression, Expression.Constant(null)), + Expression.Constant("null"), + Expression.Call( + resultMO.Expression, + typeof(object).GetMethod("GetType") + ), + typeof(object) + ) + ) + ) + ), + binder.ReturnType + ), + binder.ReturnType + ); +#else var checkedConvert = Expression.Condition( Expression.TypeIs(resultMO.Expression, binder.ReturnType), convert, @@ -524,6 +562,7 @@ namespace System.Dynamic { ), binder.ReturnType ); +#endif resultMO = new DynamicMetaObject(checkedConvert, resultMO.Restrictions); } |