/*********************************************************************/ /* */ /* AgentService Framework */ /* www.agentservice.it */ /* */ /* Copyright (C) 2007 l.i.d.o. - DIST, University of Genoa, Italy */ /* GNU Lesser General Public License */ /* */ /* This library is free software; you can redistribute it and/or */ /* modify it under the terms of the GNU Lesser General Public */ /* License as published by the Free Software Foundation, */ /* version 2.1 of the License. */ /* */ /* This library is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ /* Lesser General Public License for more details. */ /* */ /* You should have received a copy of the GNU Lesser General Public */ /* License along with this library; if not, write to the */ /* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */ /* Boston, MA 02111-1307, USA. */ /* */ /*********************************************************************/ using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters; using System.Runtime.Serialization.Formatters.Binary; namespace AgentService.Platform.MTS { /// /// Class MessageBodyItem. Represent an item of the message's body. /// [Serializable] [DataContract] public class MessageBodyItem/* : ISerializable*/ { /// /// Item's identifier /// protected string identifier; /// /// Gets the item's identifier /// [DataMember] public string Identifier { get { return this.identifier; } set { identifier = value; } } /// /// The class type of the istance /// protected System.Type type; /// /// Gets the type of the item /// public System.Type Type { get { return this.type; } set { type = value; } } /// /// BodyItem's instance /// protected object instance; /// /// Gets the instance of the body's item /// public object Instance { get { return this.instance; } set { if (value.GetType().Equals(this.type)) { this.instance = value; } else { throw new ArgumentException("Cannot assign an object: type mismatch.", "Instance"); } } } /// /// A string containing the istance type /// [DataMember] public String TypeString { get { return type.FullName; } set { Type t = System.Type.GetType(value); if (t != null) this.type = t; } } /// /// A string that contains the serialized object /// [DataMember] public String InstanceString { get { MemoryStream mem = new MemoryStream(); CompactFormatter.CompactFormatterPlus cfp = new CompactFormatter.CompactFormatterPlus(); cfp.Serialize(mem, this.instance); mem.Close(); byte[] b = mem.ToArray(); return Convert.ToBase64String(b); } set { byte[] b = Convert.FromBase64String(value); MemoryStream ms = new MemoryStream(b); ms.Position = 0; CompactFormatter.CompactFormatterPlus cfp = new CompactFormatter.CompactFormatterPlus(); this.instance = cfp.Deserialize(ms); ms.Close(); this.type = this.instance.GetType(); } } /// /// Constructs a MessageBodyItem. /// public MessageBodyItem() { this.identifier = null; this.type = null; this.instance = null; } /// /// Constructs a MEssagBodyItem with the given identifier, type, and istance. /// /// Item's Identifier /// Type of the istance /// The object item's istance public MessageBodyItem(string identifier, System.Type type, object instance) { if (type.IsSerializable == false) throw new System.ArgumentException("the type of the item is not serializable", "type"); if (type.IsInstanceOfType(instance) == false) throw new System.ArgumentException("the instance is not of the provided type", "instance"); this.identifier = identifier; this.type = type; this.instance = instance; } /// /// Deserialization Constructor. /// /// SerializeInfo /// The context in which deserializing public MessageBodyItem(SerializationInfo info, StreamingContext context) { identifier = (string)info.GetValue("identifier", typeof(string)); type = (System.Type)info.GetValue("type", typeof(System.Type)); instance = (object)info.GetValue("instance",typeof(object)); } /// /// Serialization function. /// /// SerializeInfo /// The context in which deserializing public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("identifier", this.identifier); info.AddValue("type", this.type); info.AddValue("instance", this.instance); } } }