Imported Upstream version 4.6.0.243

Former-commit-id: ff34202749e8df2aa83f2578b16260b444f50987
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2016-09-09 07:19:48 +00:00
parent 804b15604f
commit 3cc9601fd9
113 changed files with 1476 additions and 4439 deletions

View File

@@ -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);
}