Search code examples
c#unit-testingdatacontractserializer

How can I handle two references to assemblies that have the same exports?


I just learnt about supporting roundtrip in serialization via DataContract. As I'm sure I will be making mistakes with this mechanism, I want to establish a unit test in Visual Studio to test whether the new versions of my files are ok.

In order to achieve this, I would like to

  1. create a document object of the latest version in the unit test
  2. Then serialize this object into a MemoryStream
  3. Deserialize this object with an old version of my application
  4. Serialize this old object to the MemoryStream
  5. Deserialize this object with the current version
  6. Check whether the properties of the first object and the last object are identical

This sounds simple (or maybe not?), my problem is now that the unit test needs a reference to the current version of my application and to an old version of my application at the same time.

So if I now try to create this object, I get an error saying that this type is defined in two assemblies.

So my question is, how can I handle two references to assemblies that have the same exports.

Can I do something like

OldAssembly.MyClass old;
CurrentAssembly.MyClass new;

Solution

  •   > how can I handle two references to assemblies that have the same exports.
    

    You can declare an extern alias for every referenced assembly. This alias is usually global but you can declare your own either as commandline-argument for the compiler or in visual studio under ReferencedAssemlys/{MyAssemly}/Properties/Aliase.

    In you Program you can use

    old::My.NameSpace.MyClass old;
    global::My.NameSpace.MyClass new;