Implementation of System.Reflection.ParameterInfo.HasDefaultValue
authorBart Vries <b.vries@sanba.nl>
Thu, 2 Jan 2014 23:15:52 +0000 (00:15 +0100)
committerBart Vries <b.vries@sanba.nl>
Thu, 2 Jan 2014 23:15:52 +0000 (00:15 +0100)
mcs/class/corlib/System.Reflection/MonoParameterInfo.cs
mcs/class/corlib/System.Reflection/ParameterInfo.cs
mcs/class/corlib/Test/System.Reflection/ParameterInfoTest.cs

index 327166e4488c3dd2bef30c5db266ece13db30b3b..ce4fdda324c6944a7fda60656fcdcfae736cef70 100644 (file)
@@ -236,5 +236,20 @@ namespace System.Reflection
                                return Type.EmptyTypes;
                        return types;
                }
+
+#if NET_4_5
+               public override bool HasDefaultValue {
+                       get { 
+                               object defaultValue = DefaultValue;
+                               if (defaultValue == null)
+                                       return true;
+
+                               if (defaultValue.GetType () == typeof(DBNull) || defaultValue.GetType () == typeof(Missing))
+                                       return false;
+
+                               return true;
+                       }
+               }
+#endif
        }
 }
index 890dffd3b2f95c63a9650990eb32f10977c87b41..1c00fb96c10c03d9dd71430504032ae5f5f9a075 100644 (file)
@@ -193,8 +193,7 @@ namespace System.Reflection
                public virtual IEnumerable<CustomAttributeData> CustomAttributes {
                        get { return GetCustomAttributesData (); }
                }
-
-               [MonoTODO]
+               
                public virtual bool HasDefaultValue {
                        get { throw new NotImplementedException (); }
                }
index ff1d915632d1755ab027e9d4de6eeec46852eec0..b22f46f36507cf03144467ebacdb9adedc85f3be 100644 (file)
@@ -98,6 +98,15 @@ namespace MonoTests.System.Reflection
                        Assert.AreEqual (ParamEnum.Foo, info [5].DefaultValue, "#2");
                }
 
+#if NET_4_5
+               [Test]
+               public void HasDefaultValueEnum () {
+                       ParameterInfo[] info = typeof (ParameterInfoTest).GetMethod ("paramMethod").GetParameters ();
+
+                       Assert.IsTrue (info [5].HasDefaultValue);
+               }
+#endif
+
                public static void Sample2 ([DecimalConstantAttribute(2,2,2,2,2)] decimal a, [DateTimeConstantAttribute(123456)] DateTime b) {}
 
                [Test]
@@ -117,7 +126,19 @@ namespace MonoTests.System.Reflection
                        Assert.AreEqual (pi [1].DefaultValue.GetType (), typeof (Missing), "#2");
                }
 
-               public void Sample (int a, [Optional] int b)
+#if NET_4_5
+               [Test]
+               public void TestHasDefaultValues ()
+               {
+                       ParameterInfo [] pi = typeof (ParameterInfoTest).GetMethod ("Sample").GetParameters ();
+
+                       Assert.IsFalse (pi [0].HasDefaultValue, "#1");
+                       Assert.IsFalse (pi [1].HasDefaultValue, "#2");
+                       Assert.IsTrue (pi [2].HasDefaultValue, "#3");
+               }
+#endif
+
+               public void Sample (int a, [Optional] int b, object c = null)
                {
                }
 
@@ -235,6 +256,14 @@ namespace MonoTests.System.Reflection
                        Assert.AreEqual (decimal.MaxValue, info [0].DefaultValue);
                }
 
+#if NET_4_5
+               [Test]
+               public void HasDefaultValueDecimal () {
+                       var info = typeof (ParameterInfoTest).GetMethod ("TestC").GetParameters ();
+                       Assert.IsTrue (info [0].HasDefaultValue);
+               }
+#endif
+
                class MyParameterInfo2 : ParameterInfo
                {
                        public ParameterAttributes MyAttrsImpl;