/*
OpenNxSerialization Framework
Copyright (C) 2006 - 2008 "NeXtreme Innovations"
[The Next Xtreme Innovations]
This program is free software, distributed under the terms of
the GNU General Public License Version 2. See the "License.txt" file
at the top of the source tree.
*/
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using NeXtreme.OpenNxSerialization.IO;
using NeXtreme.OpenNxSerialization.Formatters;
namespace NeXtreme.OpenNxSerialization.Surrogates
{
/// <summary>
/// Surrogate for <see cref="System.Runtime.Serialization.ISerializable"/> type.
/// </summary>
sealed class NxObjRefSerializationSurrogate : NxSerializationSurrogateBase, INxSerializationSurrogate, INxBuiltinSerializationSurrogate
{
/// <summary> The constructor used to create instance of surrogated type </summary>
private ConstructorInfo mConstructor;
public NxObjRefSerializationSurrogate() : base(typeof(ObjRef))
{
mConstructor = ActualType.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
null,
new Type[] { typeof(SerializationInfo), typeof(StreamingContext) },
null);
if (mConstructor == null)
{
throw new ArgumentException(String.Format(Resources.Surrogates_NoMatchCons, ActualType.Name));
}
}
public object Read(INxBinaryReader reader)
{
SerializationInfo info = (SerializationInfo)reader.ReadObjectAs(typeof(SerializationInfo));
return mConstructor.Invoke(new object[] { info, reader.Context.StreamingContext });
}
public void Write(INxBinaryWriter writer, object graph)
{
SerializationInfo info = new SerializationInfo(ActualType, writer.Context.FormatConverter);
((ObjRef)graph).GetObjectData(info, writer.Context.StreamingContext);
writer.WriteObjectAs(info, typeof(SerializationInfo));
}
}
/// <summary>
/// Surrogate for <see cref="MarshalByRefObject"/> type.
/// </summary>
sealed class NxMarshalByRefObjectSerializationSurrogate : NxSerializationSurrogateBase, INxSerializationSurrogate, INxBuiltinSerializationSurrogate
{
/// <summary> The constructor used to create instance of surrogated type </summary>
private ConstructorInfo mConstructor;
public NxMarshalByRefObjectSerializationSurrogate(Type type) : base(type)
{
mConstructor = typeof(ObjRef).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
null,
new Type[] { typeof(SerializationInfo), typeof(StreamingContext) },
null);
if (mConstructor == null)
{
throw new ArgumentException(String.Format(Resources.Surrogates_NoMatchCons, ActualType.Name));
}
}
public object Read(INxBinaryReader reader)
{
SerializationInfo info = (SerializationInfo)reader.ReadObjectAs(typeof(SerializationInfo));
ObjRef refObj = (ObjRef)mConstructor.Invoke(new object[] { info, reader.Context.StreamingContext });
if (refObj != null)
{
return RemotingServices.Unmarshal(refObj);
}
return null;
}
public void Write(INxBinaryWriter writer, object graph)
{
SerializationInfo info = new SerializationInfo(ActualType, writer.Context.FormatConverter);
if (RemotingServices.IsTransparentProxy(graph))
{
RemotingServices.GetRealProxy(graph).GetObjectData(info, writer.Context.StreamingContext);
}
else
{
RemotingServices.GetObjectData(graph, info, writer.Context.StreamingContext);
}
writer.WriteObjectAs(info, typeof(SerializationInfo));
}
}
/// <summary>
/// Surrogate for <see cref="Header"/> type.
/// </summary>
sealed class NxHeaderSerializationSurrogate : NxSerializationSurrogate, INxBuiltinSerializationSurrogate
{
public NxHeaderSerializationSurrogate() : base(typeof(Header))
{
}
public override object Instantiate(INxBinaryReader reader)
{
return new Header(null, null);
}
public override object ReadDirect(INxBinaryReader reader, object graph)
{
Header header = (Header)graph;
header.Name = reader.ReadString();
header.HeaderNamespace = reader.ReadString();
header.MustUnderstand = reader.ReadBoolean();
header.Value = reader.ReadObject();
return graph;
}
public override void WriteDirect(INxBinaryWriter writer, object graph)
{
Header header = (Header)graph;
writer.Write(header.Name);
writer.Write(header.HeaderNamespace);
writer.Write(header.MustUnderstand);
writer.WriteObject(header.Value);
}
}
/// <summary>
/// Surrogate for <see cref="Header"/>[] type.
/// </summary>
sealed class NxHeaderArraySerializationSurrogate : NxSerializationSurrogate, INxBuiltinSerializationSurrogate
{
public NxHeaderArraySerializationSurrogate() : base(typeof(Header[])) { }
public override object Instantiate(INxBinaryReader reader)
{
int length = reader.ReadInt32();
return new Header[length];
}
public override object ReadDirect(INxBinaryReader reader, object graph)
{
Header[] array = (Header[])graph;
for (int i = 0; i < array.Length; i++)
array[i] = (Header) reader.ReadObject();
return array;
}
public override void WriteDirect(INxBinaryWriter writer, object graph)
{
Header[] array = (Header[])graph;
writer.Write(array.Length);
for (int i = 0; i < array.Length; i++)
writer.WriteObject(array[i]);
}
}
}