[Mono-dev] [PATCH] Setting by Reflection a literal field

Zoltan Varga vargaz at gmail.com
Sat Sep 30 07:07:26 EDT 2006


Hi,

 I think this is ok to check in. 1.1.13 is supposedly only open for
critical bug fixes.

               Zoltan

On 9/29/06, Jb Evain <mono at evain.net> wrote:
> Hi,
>
> Here is a patch to throw a FieldAccessException instead of hitting an
> assert in the runtime when someone tries to set a value to a literal
> field using Reflection.
>
> Is it ok to commit?
>
> If so, should I commit it to the 1.1.13 branch also?
>
> Jb
>
>
>
> Index: Test/System.Reflection/ChangeLog
> ===================================================================
> --- Test/System.Reflection/ChangeLog    (revision 64207)
> +++ Test/System.Reflection/ChangeLog    (working copy)
> @@ -1,3 +1,7 @@
> +2006-09-29  Jb Evain  <jbevain at gmail.com>
> +
> +       * FieldInfoTest.cs: Test for FieldInfo.SetValue on a literal field.
> +
>  2006-08-08  Gert Driesen  <drieseng at users.sourceforge.net>
>
>         * AssemblyTest.cs: Modified test to pass on 2.0 profile and .NET 2.0.
> Index: Test/System.Reflection/FieldInfoTest.cs
> ===================================================================
> --- Test/System.Reflection/FieldInfoTest.cs     (revision 64207)
> +++ Test/System.Reflection/FieldInfoTest.cs     (working copy)
> @@ -118,6 +118,16 @@
>                 f.SetValue (null, 8);
>         }
>
> +       const int literal = 42;
> +
> +       [Test]
> +       [ExpectedException (typeof (FieldAccessException))]
> +       public void SetValueOnLiteralField ()
> +       {
> +               FieldInfo f = typeof (FieldInfoTest).GetField ("literal", BindingFlags.Static | BindingFlags.NonPublic);
> +               f.SetValue (null, 0);
> +       }
> +
>         public int? nullable_field;
>
>         public static int? static_nullable_field;
> Index: System.Reflection/ChangeLog
> ===================================================================
> --- System.Reflection/ChangeLog (revision 64207)
> +++ System.Reflection/ChangeLog (working copy)
> @@ -1,3 +1,7 @@
> +2006-09-29  Jb Evain  <jbevain at gmail.com>
> +
> +       * MonoFieldInfo.cs: throw a FieldAccessException when setting a literal field.
> +
>  2006-08-08  Gert Driesen  <drieseng at users.sourceforge.net>
>
>         * Assembly.cs: On 2.0 profile, throw FileNotFoundException for
> Index: System.Reflection/MonoField.cs
> ===================================================================
> --- System.Reflection/MonoField.cs      (revision 64207)
> +++ System.Reflection/MonoField.cs      (working copy)
> @@ -117,6 +117,8 @@
>                 {
>                         if (!IsStatic && obj == null)
>                                 throw new TargetException ("Non-static field requires a target");
> +                       if (IsLiteral)
> +                               throw new FieldAccessException ("Cannot set a constant field");
>                         if (binder == null)
>                                 binder = Binder.DefaultBinder;
>                         if (val != null) {
>
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
>
>



More information about the Mono-devel-list mailing list