Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>MEF will definitely help you, but not only it. You have to use the <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.iserializationsurrogate.aspx" rel="nofollow">ISerializationSurrogate</a>. Most of the explanations for the below you can find <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.surrogateselector.aspx" rel="nofollow">here</a>.</p> <p>So, given the following definition for the packet interface:</p> <pre><code>public interface IPacket { string GetInfo(); } </code></pre> <p>You have the following implementations, residing in their own assembly:</p> <pre><code>[Export(typeof(IPacket))] class FirstPacket : IPacket { public FirstPacket() { Name = "Joe"; } public string Name { get; set; } public string GetInfo() { return "Name: " + Name; } } [Export(typeof(IPacket))] class SecondPacket : IPacket { public SecondPacket() { Measurement = 42.42m; } public decimal Measurement { get; set; } public string GetInfo() { return "Measurement: " + Measurement; } } </code></pre> <p>Now we will define another interface, something like:</p> <pre><code>public interface IPacketSurrogateProvider { void AddSurrogate(SurrogateSelector toSelector); } </code></pre> <p>And the matching implementations, in the same assembly where concrete packets are defined:</p> <pre><code>[Export(typeof(IPacketSurrogateProvider))] class FirstPacketSurrogateProvider : IPacketSurrogateProvider, ISerializationSurrogate { public void AddSurrogate(SurrogateSelector toSelector) { toSelector.AddSurrogate(typeof(FirstPacket), new StreamingContext(StreamingContextStates.All), this); } public void GetObjectData(object obj, SerializationInfo info, StreamingContext context) { info.AddValue("Name", ((FirstPacket)obj).Name); } public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) { ((FirstPacket)obj).Name = info.GetString("Name"); return obj; } } [Export(typeof(IPacketSurrogateProvider))] class SecondPacketSurrogateProvider : IPacketSurrogateProvider, ISerializationSurrogate { public void AddSurrogate(SurrogateSelector toSelector) { toSelector.AddSurrogate(typeof(SecondPacket), new StreamingContext(StreamingContextStates.All), this); } public void GetObjectData(object obj, SerializationInfo info, StreamingContext context) { info.AddValue("Measurement", ((SecondPacket)obj).Measurement); } public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector) { ((SecondPacket)obj).Measurement = info.GetDecimal("Measurement"); return obj; } } </code></pre> <p>And now, in an assembly which does have reference to the one with the interfaces, but not to the one with the implementations, and having the same deployment folder as both of the above:</p> <pre><code>public static void Test() { var container = new CompositionContainer(new DirectoryCatalog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))); var packets = container.GetExportedValues&lt;IPacket&gt;().ToArray(); var packetSurrogateProviders = container.GetExportedValues&lt;IPacketSurrogateProvider&gt;(); var surrogateSelector = new SurrogateSelector(); foreach (var provider in packetSurrogateProviders) { provider.AddSurrogate(surrogateSelector); } var deserializedPackets = new IPacket[] { }; using (var stream = new MemoryStream()) { var formatter = new BinaryFormatter {SurrogateSelector = surrogateSelector}; formatter.Serialize(stream, packets); stream.Position = 0; deserializedPackets = (IPacket[])formatter.Deserialize(stream); } foreach (var packet in deserializedPackets) { Console.WriteLine("Packet info: {0}", packet.GetInfo()); } } </code></pre> <p>Which produces:</p> <blockquote> <p>Packet info: Name: Joe</p> <p>Packet info: Measurement: 42.42</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload