From 1d08b24a11e2e5a1ab33803a7aee1c081f39bdf1 Mon Sep 17 00:00:00 2001 From: Atsushi Eno Date: Mon, 16 May 2011 16:31:14 +0900 Subject: [PATCH] Attachable property setter was not working with XamlObjectWriter. Fixed bug #693779. --- .../System.Xaml.Schema/XamlMemberInvoker.cs | 5 +++- .../System.Xaml/XamlObjectWriter.cs | 2 +- .../Test/System.Xaml/TestedTypes.cs | 13 ++++++++++ .../Test/System.Xaml/XamlObjectWriterTest.cs | 17 +++++++++++++ .../Test/System.Xaml/XamlTypeTest.cs | 24 +++++++++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) diff --git a/mcs/class/System.Xaml/System.Xaml.Schema/XamlMemberInvoker.cs b/mcs/class/System.Xaml/System.Xaml.Schema/XamlMemberInvoker.cs index dffed24ca71..83840552ae6 100644 --- a/mcs/class/System.Xaml/System.Xaml.Schema/XamlMemberInvoker.cs +++ b/mcs/class/System.Xaml/System.Xaml.Schema/XamlMemberInvoker.cs @@ -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) diff --git a/mcs/class/System.Xaml/System.Xaml/XamlObjectWriter.cs b/mcs/class/System.Xaml/System.Xaml/XamlObjectWriter.cs index 349ef573d20..deed5214070 100644 --- a/mcs/class/System.Xaml/System.Xaml/XamlObjectWriter.cs +++ b/mcs/class/System.Xaml/System.Xaml/XamlObjectWriter.cs @@ -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); } diff --git a/mcs/class/System.Xaml/Test/System.Xaml/TestedTypes.cs b/mcs/class/System.Xaml/Test/System.Xaml/TestedTypes.cs index aa8e3ed21bf..acf3711be49 100755 --- a/mcs/class/System.Xaml/Test/System.Xaml/TestedTypes.cs +++ b/mcs/class/System.Xaml/Test/System.Xaml/TestedTypes.cs @@ -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; diff --git a/mcs/class/System.Xaml/Test/System.Xaml/XamlObjectWriterTest.cs b/mcs/class/System.Xaml/Test/System.Xaml/XamlObjectWriterTest.cs index d1138c7286c..53e2e7b2313 100755 --- a/mcs/class/System.Xaml/Test/System.Xaml/XamlObjectWriterTest.cs +++ b/mcs/class/System.Xaml/Test/System.Xaml/XamlObjectWriterTest.cs @@ -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 (@"", 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] diff --git a/mcs/class/System.Xaml/Test/System.Xaml/XamlTypeTest.cs b/mcs/class/System.Xaml/Test/System.Xaml/XamlTypeTest.cs index 153d3c75c72..923d6d43630 100755 --- a/mcs/class/System.Xaml/Test/System.Xaml/XamlTypeTest.cs +++ b/mcs/class/System.Xaml/Test/System.Xaml/XamlTypeTest.cs @@ -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 () { -- 2.25.1