5 // Peter Van Isacker (sclytrack@planetinternet.be)
6 // Rafael Teixeira (rafaelteixeirabr@hotmail.com)
8 // (C) 2003 Peter Van Isacker
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
34 using System.ComponentModel;
36 using System.Xml.Serialization;
39 namespace System.Messaging
41 public class XmlMessageFormatter: IMessageFormatter, ICloneable
43 private ArrayList targetTypes = new ArrayList ();
45 public XmlMessageFormatter()
49 public XmlMessageFormatter(string[] targetTypeNames) : this (GetTypesFromNames (targetTypeNames))
53 public XmlMessageFormatter(Type[] targetTypes)
55 this.targetTypes = new ArrayList (targetTypes);
58 private static Type[] GetTypesFromNames(string[] targetTypeNames)
60 Type[] ts = new Type[targetTypeNames.Length];
61 for (int i = 0; i < targetTypeNames.Length; i++)
62 ts[i] = Type.GetType (targetTypeNames[i]);
67 [MessagingDescription ("XmlMsgTargetTypeNames")]
68 public string[] TargetTypeNames
72 if (targetTypes == null)
75 ArrayList listOfNames = new ArrayList (targetTypes.Count);
76 foreach (Type type in targetTypes)
77 listOfNames.Add (type.FullName);
78 return (string[]) listOfNames.ToArray (typeof (string));
80 set { TargetTypes = GetTypesFromNames (value); }
84 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
85 [MessagingDescription ("XmlMsgTargetTypes")]
86 public Type[] TargetTypes
88 get { return (Type[]) targetTypes.ToArray (typeof (Type)); }
89 set { targetTypes = new ArrayList (value); }
93 public bool CanRead(Message message)
95 // Need an implementation of XmlNodeReader.
96 throw new NotImplementedException();
101 return new XmlMessageFormatter((Type[])TargetTypes.Clone());
104 private void AddType (Type t)
109 public object Read(Message message)
111 message.BodyStream.Seek (0, SeekOrigin.Begin);
112 object result = null;
113 foreach (Type t in targetTypes) {
114 XmlSerializer serializer = new XmlSerializer (t);
116 return serializer.Deserialize (message.BodyStream);
117 } catch (InvalidOperationException e) {
118 Console.WriteLine (e);
121 string error = "Unable to deserialize message body. Type is not one of: "
122 + String.Join (",", TargetTypeNames);
123 throw new InvalidOperationException (error);
126 public void Write(Message message, object obj)
129 throw new ArgumentNullException ();
131 Stream stream = message.BodyStream;
132 if (stream == null) {
133 stream = new MemoryStream ();
134 message.BodyStream = stream;
137 XmlSerializer serializer = new XmlSerializer (obj.GetType ());
138 serializer.Serialize (stream, obj);
140 message.BodyType = (int) FormatterTypes.Xml;
141 AddType (obj.GetType ());