binary serialization of types defined in a silverlight assembly
I have a silverlight assembly which is referenced by a silverlight application and a ASP.Net application.
In the ASP.Net app i need to serialize(Binary) some of the instances of types defined in the silverlight assembly. Can anyone help me with this?
Edit
My problem is that i have a silverlight assembly which is used by a silverilght application, a asp.net app and a 开发者_开发技巧winforms app, in my asp.net app and winforms app i need to serialize types defined in the shared silverlight assmebly.
Also on a slightly different note can some one explain why this attribute is needed?
Also on a slightly different note can some one explain why this attribute is needed?
Lets see: During serialization BinaryFormatter calls methods of FormatterServices class. One of they is
private static MemberInfo[] InternalGetSerializableMembers(RuntimeType type)
This method contains next code:
if (!CheckSerializable(type))
{
throw new SerializationException(Environment.GetResourceString("Serialization_NonSerType", new object[] { type.FullName, type.Module.Assembly.FullName }));
}
lets check CheckSerializable method of FormatterServices class:
private static bool CheckSerializable(RuntimeType type)
{
return type.IsSerializable;
}
This code similar to:
Type t = typeof (SomeClass);
bool isTypeSerializable = t.IsSerializable;
In this example isTypeSerializable will be true if class SomeClass has SerializableAttribute. Otherwise false.
Soo... Simple answer: this attribute is flag which indicates that instances of class may be serialized. So it just needed and thats all.
Regarding your main question:
Unfortunately you can't use binary formatter for instance of class defined in silverlight assembly directly.
One way to do that to use a proxy class.
Example:
Class inside your silverlight assembly:
public class SomeClass
{
public int IntValue { get; set; }
public string StringValue { get; set; }
public bool BoolValue { get; set; }
}
Proxy class which supports serialization inside common .NET assemly with reference to silverlight assembly:
[Serializable]
public class SomeClassProxy
{
public int IntValue { get; set; }
public string StringValue { get; set; }
public bool BoolValue { get; set; }
public static SomeClassProxy GetSerializableObject(SomeClass silverlightClass)
{
return new SomeClassProxy
{
IntValue = silverlightClass.IntValue,
StringValue = silverlightClass.StringValue,
BoolValue = silverlightClass.BoolValue
};
}
public static SomeClass DeserializeSilverlightCompatible(Stream stream)
{
BinaryFormatter formatter = new BinaryFormatter();
SomeClassProxy proxy = formatter.Deserialize(stream) as SomeClassProxy;
return new SomeClass
{
IntValue = proxy.IntValue,
StringValue = proxy.StringValue,
BoolValue = proxy.BoolValue
};
}
}
So in your asp .net or winforms applications you should operate with proxy class:
using(MemoryStream memoryStream = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
//create instance of silverlight class
SomeClass mySilverlightClass = new SomeClass();
mySilverlightClass.IntValue = 555;
mySilverlightClass.StringValue = "Hello World!";
mySilverlightClass.BoolValue = true;
//<===serialize and take care of silverlight instance
formatter.Serialize(memoryStream, SomeClassProxy.GetSerializableObject(mySilverlightClass));
memoryStream.Seek(0, SeekOrigin.Begin);
//===>deserialize to silverlight instance
SomeClass mySilverlightClassRestored = SomeClassProxy.DeserializeSilverlightCompatible(memoryStream);
}
So this proxy takes care on both serialization and deserialization (after deserialization you'll receive instance of class SomeClass which defined in silverlight assembly).
If binary formatter is soft rescriction then I may recommend you to use xml serializer instead:
XmlSerializer s = new XmlSerializer(typeof(SomeClass));
s.Serialize(memoryStream, mySilverlightClass);
memoryStream.Seek(0, SeekOrigin.Begin);
SomeClass restored = s.Deserialize(memoryStream) as SomeClass;
It that case SerializableAttribute isn't needed.
You could try out my serializer which is capable of moving binary data between .NET and Silverlight... http://whydoidoit.com
I used something called the sharp serializer which is able to binary serialize types without a serializable attribute.
You can use #if
directives to include serialization attributes (and code) for non-Silverlight builds:
#if !SILVERLIGHT
[Serializable]
#endif
public class SomeClass
{
}
精彩评论