From b3c280bb493afaf552bd9db8cf31b3129de72c21 Mon Sep 17 00:00:00 2001 From: Iain McCoy Date: Sun, 28 Aug 2005 12:27:42 +0000 Subject: 2005-08-28 Iain McCoy * System.Windows.Serialization/Parser.cs: a bit more refactoring to reduce code duplication * Mono.Windows.Serialization/ParserToCode.cs: similarly, refactoring to reduce code duplication svn path=/trunk/mcs/; revision=49023 --- mcs/class/PresentationFramework/ChangeLog | 7 +++++ .../Mono.Windows.Serialization/ParserToCode.cs | 21 ++++++------- .../System.Windows.Serialization/Parser.cs | 34 ++++++++++------------ 3 files changed, 34 insertions(+), 28 deletions(-) (limited to 'mcs/class/PresentationFramework') diff --git a/mcs/class/PresentationFramework/ChangeLog b/mcs/class/PresentationFramework/ChangeLog index 980ce218bd4..714d24624c5 100644 --- a/mcs/class/PresentationFramework/ChangeLog +++ b/mcs/class/PresentationFramework/ChangeLog @@ -1,3 +1,10 @@ +2005-08-28 Iain McCoy + + * System.Windows.Serialization/Parser.cs: a bit more refactoring to + reduce code duplication + * Mono.Windows.Serialization/ParserToCode.cs: similarly, refactoring + to reduce code duplication + 2005-08-28 Iain McCoy * Mono.Windows.Serialization/ParserConsumerBase.cs: move dispatching diff --git a/mcs/class/PresentationFramework/Mono.Windows.Serialization/ParserToCode.cs b/mcs/class/PresentationFramework/Mono.Windows.Serialization/ParserToCode.cs index 2c0e526948b..95818a309ad 100644 --- a/mcs/class/PresentationFramework/Mono.Windows.Serialization/ParserToCode.cs +++ b/mcs/class/PresentationFramework/Mono.Windows.Serialization/ParserToCode.cs @@ -231,11 +231,7 @@ namespace Mono.Windows.Serialization { public override void CreateEventDelegate(string functionName, Type eventDelegateType) { debug(); - CodeExpression expr = new CodeObjectCreateExpression( - eventDelegateType, - new CodeMethodReferenceExpression( - new CodeThisReferenceExpression(), - functionName)); + CodeExpression expr = buildDelegate(eventDelegateType, functionName); CodeAttachEventStatement attach = new CodeAttachEventStatement( (CodeEventReferenceExpression)peek(), expr); @@ -246,16 +242,21 @@ namespace Mono.Windows.Serialization { public override void CreatePropertyDelegate(string functionName, Type propertyType) { debug(); - CodeExpression expr = new CodeObjectCreateExpression( - propertyType, - new CodeMethodReferenceExpression( - new CodeThisReferenceExpression(), - functionName)); + CodeExpression expr = buildDelegate(propertyType, functionName); CodeAssignStatement assignment = new CodeAssignStatement( (CodeExpression)peek(), expr); constructor.Statements.Add(assignment); } + private CodeExpression buildDelegate(Type propertyType, string functionName) + { + return new CodeObjectCreateExpression( + propertyType, + new CodeMethodReferenceExpression( + new CodeThisReferenceExpression(), + functionName)); + + } private CodeExpression fetchConverter(Type propertyType) { diff --git a/mcs/class/PresentationFramework/System.Windows.Serialization/Parser.cs b/mcs/class/PresentationFramework/System.Windows.Serialization/Parser.cs index 9477cb877fc..b715ffffce7 100644 --- a/mcs/class/PresentationFramework/System.Windows.Serialization/Parser.cs +++ b/mcs/class/PresentationFramework/System.Windows.Serialization/Parser.cs @@ -96,7 +96,6 @@ namespace System.Windows.Serialization { MethodInfo setter = attachedTo.GetMethod("Set" + propertyName); - Console.WriteLine(attachedTo.GetType()+ ".Set" + propertyName + "(" + peek().GetType() + ", " + value.GetType() + ")"); setter.Invoke(null, new object[] { peek(), value}); } @@ -122,11 +121,7 @@ namespace System.Windows.Serialization { public override void CreatePropertyText(string text, Type propertyType) { - object value = text; - if (propertyType != typeof(string)) { - TypeConverter tc = TypeDescriptor.GetConverter(propertyType); - value = tc.ConvertFromString(text); - } + object value = convertText(propertyType, text); storeToProperty(value); } @@ -138,7 +133,7 @@ namespace System.Windows.Serialization { } public override void EndPropertyObject(Type destType) { - object value = convertPropertyObjectValue(destType); + object value = convertPropertyObjectValue(destType, pop()); storeToProperty(value); } private void storeToProperty(object value) @@ -147,17 +142,24 @@ namespace System.Windows.Serialization { object o = peek(1); p.SetValue(o, value, null); } - private object convertPropertyObjectValue(Type destType) + private object convertPropertyObjectValue(Type destType, object value) { - object value = pop(); Type sourceType = value.GetType(); - Debug.WriteLine("ObjectWriter: EndPropertyObject has a " + value + value.GetType() + ", needs a " + destType); if (destType != sourceType && !sourceType.IsSubclassOf(destType)) { TypeConverter tc = TypeDescriptor.GetConverter(sourceType); value = tc.ConvertTo(value, destType); } return value; } + private object convertText(Type propertyType, string text) + { + if (propertyType != typeof(string)) { + TypeConverter tc = TypeDescriptor.GetConverter(propertyType); + return tc.ConvertFromString(text); + } else { + return text; + } + } public override void CreateDependencyPropertyObject(Type type, string name) { @@ -165,17 +167,13 @@ namespace System.Windows.Serialization { } public override void EndDependencyPropertyObject(Type finalType) { - push(convertPropertyObjectValue(finalType)); + push(convertPropertyObjectValue(finalType, pop())); } // top of stack is reference to an attached property public override void CreateDependencyPropertyText(string text, Type propertyType) { - object value = text; - if (propertyType != typeof(string)) { - TypeConverter tc = TypeDescriptor.GetConverter(propertyType); - value = tc.ConvertFromString(text); - } + object value = convertText(propertyType, text); push(value); } @@ -215,12 +213,12 @@ namespace System.Windows.Serialization { { object v = objects[objects.Count - 1]; objects.RemoveAt(objects.Count - 1); - Debug.WriteLine("ObjectWriter POPPING"); + Debug.WriteLine("ObjectWriter: POPPING"); return v; } private void push(object v) { - Debug.WriteLine("ObjectWriter PUSHING " + v); + Debug.WriteLine("ObjectWriter: PUSHING " + v); objects.Add(v); } } -- cgit v1.2.3