Attachable property setter was not working with XamlObjectWriter.
authorAtsushi Eno <atsushi@ximian.com>
Mon, 16 May 2011 07:31:14 +0000 (16:31 +0900)
committerAtsushi Eno <atsushi@ximian.com>
Mon, 16 May 2011 07:31:14 +0000 (16:31 +0900)
Fixed bug #693779.

mcs/class/System.Xaml/System.Xaml.Schema/XamlMemberInvoker.cs
mcs/class/System.Xaml/System.Xaml/XamlObjectWriter.cs
mcs/class/System.Xaml/Test/System.Xaml/TestedTypes.cs
mcs/class/System.Xaml/Test/System.Xaml/XamlObjectWriterTest.cs
mcs/class/System.Xaml/Test/System.Xaml/XamlTypeTest.cs

index dffed24ca7112d3fd535c1a0231edcb0f6001e7b..83840552ae62e9b9b6a0c9a9d5c32c29fdeeb0e6 100644 (file)
@@ -80,7 +80,10 @@ namespace System.Xaml.Schema
                                throw new NotSupportedException (String.Format ("not supported operation on directive member {0}", member));
                        if (UnderlyingSetter == null)
                                throw new NotSupportedException (String.Format ("Attempt to set value from read-only property {0}", member));
-                       UnderlyingSetter.Invoke (instance, new object [] {value});
+                       if (member.IsAttachable)
+                               UnderlyingSetter.Invoke (null, new object [] {instance, value});
+                       else
+                               UnderlyingSetter.Invoke (instance, new object [] {value});
                }
 
                public virtual ShouldSerializeResult ShouldSerializeValue (object instance)
index 349ef573d20bc95acd591ba5de6573d540cc68a4..deed521407018815973368ddd8ea69ea939e9730 100644 (file)
@@ -383,7 +383,7 @@ namespace System.Xaml
                        else if (member.IsDirective)
                                return;
                        else if (member.IsAttachable)
-                               AttachablePropertyServices.SetProperty (object_states.Peek ().Value, new AttachableMemberIdentifier (member.DeclaringType.UnderlyingType, member.Name), value);
+                               member.Invoker.SetValue (object_states.Peek ().Value, value);
                        else if (!source.OnSetValue (this, member, value))
                                member.Invoker.SetValue (object_states.Peek ().Value, value);
                }
index aa8e3ed21bf4ef67cda4d4e0009966cdfddb2eaa..acf3711be4963bdeacda9fdbfa54d6c737af2199 100755 (executable)
@@ -737,6 +737,19 @@ namespace MonoTests.System.Xaml
        {
        }
 
+       public class Attached2
+       {
+               internal String Property { get; set; }
+       }
+       
+       public class AttachedWrapper3
+       {
+               public static void SetProperty (Attached2 a, string value)
+               {
+                       a.Property = value;
+               }
+       }
+
        public class EventStore
        {
                public bool Method1Invoked;
index d1138c7286c7c7d797342cc4f17febca518e4fd7..53e2e7b2313bf5d21a82500843560227e035f43c 100755 (executable)
@@ -689,6 +689,23 @@ namespace MonoTests.System.Xaml
                        xw.WriteEndObject ();
                }
 
+               [Test]
+               public void WriteAttachableProperty ()
+               {
+                       Attached2 result = null;
+                       
+                       var rsettings = new XamlXmlReaderSettings ();
+                       using (var reader = new XamlXmlReader (new StringReader (String.Format (@"<Attached2 AttachedWrapper3.Property=""Test"" xmlns=""clr-namespace:MonoTests.System.Xaml;assembly={0}""></Attached2>", typeof (AttachedWrapper3).Assembly.GetName ().Name)), rsettings)) {
+                               var wsettings = new XamlObjectWriterSettings ();
+                               using (var writer = new XamlObjectWriter (reader.SchemaContext, wsettings)) {
+                                       XamlServices.Transform (reader, writer, false);
+                                       result = (Attached2) writer.Result;
+                               }
+                       }
+
+                       Assert.AreEqual ("Test", result.Property, "#1");
+               }
+
                // extra use case based tests.
 
                [Test]
index 153d3c75c72028452c97428f29ef1fd8ca10c9bd..923d6d43630e7cca44319633c701fccf8ffe9fcb 100755 (executable)
@@ -775,6 +775,30 @@ namespace MonoTests.System.Xaml
                        Assert.IsTrue (x.IsEvent, "#6");
                }
 
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void AttachablePropertySetValueNullObject ()
+               {
+                       var xt = new XamlType (typeof (Attachable), sctx);
+                       var apl = xt.GetAllAttachableMembers ();
+                       var foo = apl.First (ap => ap.Name == "Foo");
+                       Assert.IsTrue (foo.IsAttachable, "#7");
+                       foo.Invoker.SetValue (null, "xxx");
+               }
+
+               [Test]
+               public void AttachablePropertySetValueSuccess ()
+               {
+                       var xt = new XamlType (typeof (Attachable), sctx);
+                       var apl = xt.GetAllAttachableMembers ();
+                       var foo = apl.First (ap => ap.Name == "Foo");
+                       Assert.IsTrue (foo.IsAttachable, "#7");
+                       var obj = new object ();
+                       foo.Invoker.SetValue (obj, "xxx"); // obj is non-null, so valid.
+                       // FIXME: this line should be unnecessary.
+                       AttachablePropertyServices.RemoveProperty (obj, new AttachableMemberIdentifier (foo.Type.UnderlyingType, foo.Name));
+               }
+
                [Test]
                public void ReadOnlyPropertyContainer ()
                {