瀏覽代碼

Treat better Nullable in Convert expression

svn path=/trunk/mcs/; revision=103406
Roei Erez 17 年之前
父節點
當前提交
ea2a3accc0
共有 1 個文件被更改,包括 17 次插入3 次删除
  1. 17 3
      mcs/class/System.Core/System.Linq.jvm/Math.cs

+ 17 - 3
mcs/class/System.Core/System.Linq.jvm/Math.cs

@@ -224,8 +224,15 @@ namespace System.Linq.jvm
 
         public static object ConvertToTypeChecked(object a, Type fromType, Type toType)
         {
-			if (a == null && Expression.IsNullable (toType) || !toType.IsValueType)
-				return a;
+			if (Expression.IsNullable (toType) && Expression.GetNotNullableOf (toType) == fromType)
+				return a == null ? a : Activator.CreateInstance (toType, a);
+
+			if (a == null) {
+				if (!toType.IsValueType)
+					return a;
+				if (Expression.IsNullable (fromType))
+					throw new InvalidOperationException ("Nullable object must have a value");
+			}
 			
             if (IsType(toType, a)){
                 return a;
@@ -240,8 +247,15 @@ namespace System.Linq.jvm
 
 		public static object ConvertToTypeUnchecked (object a, Type fromType, Type toType)
 		{
-			if (a == null && Expression.IsNullable (toType) || !toType.IsValueType)							
+			if (Expression.IsNullable (toType) && Expression.GetNotNullableOf (toType) == fromType)
+				return a == null ? a : Activator.CreateInstance (toType, a);
+
+			if (a == null) { 
+				if (!toType.IsValueType)
 					return a;
+				if (Expression.IsNullable (fromType))
+					throw new InvalidOperationException ("Nullable object must have a value");
+			}
 								
 			if (IsType (toType, a)) {
 				return a;