I find myself having to write the following type of code quite often:
private TypeB ConvertTypeAToTypeB(TypeA request){
const string reqNamespace = "www.xxx/yyy";
try {
//Convert TypeA --> TypeB
XmlSerializer serializer = new XmlSerializer(typeof(TypeA),
reqNamespace);
MemoryStream stream = new MemoryStream();
serializer.Serialize(stream, request);
stream.Position = 0;
XmlSerializer mySerializerLogger = new XmlSerializer(typeof(typeB), calcReqNamespace);
TypeB typeB = (TypeB)mySerializerLogger.Deserialize(stream);
return typeB;
} catch (Exception ex) {
throw ex;
}
}
It would be much better to do it in a generic way once
Would be great if we had this built into the framework, or as a snippet – which is what I’ve done.
private T Convert<T, V>(V v, string namespace) //where T:ISerializable where V:ISerializable
{
// TODO : Require that both types have the XmlRootAttribute attribute
T t = default(T);
try {
// TODO : Check that both types have the same namespace
XmlSerializer serializer = new XmlSerializer(typeof(V), namespace);
MemoryStream stream = new MemoryStream();
serializer.Serialize(stream, v);
stream.Position = 0;
XmlSerializer mySerializerLogger = new XmlSerializer(typeof(T), _namespace);
t = (T)mySerializerLogger.Deserialize(stream);
} catch (Exception e) {
throw e;
}
return t;
}
Feel much better about this now:)
I saw that the TypeForwardedToAttribute class is included in the new qualifications for microsoft enterprise development.
Having never heard of this class before, I needed to know when, where and how. So I did a search and came up with the following:
http://www.heege.net/blog/PermaLink,guid,8d076332-4fb0-44b5-a829-4c4d653de2d6.aspx
- http://notgartner.com/posts/2955.aspx
Great - I now understood 'when' and 'where', now only for the 'how'.
A simple solution that implements the need for this assembly attribute. Client references assembly A which has type TypeA in it. Move the type from assembly A to assembly B, with attribute added to assemblyinfo file of assembly A --> compile A, which will compile B --> great, my client should still work...
Client continues to throw a TypeLoadException when instantiating the type expected to be in assembly A. I've ensured that I was not building the client executable when building assembly A, by referencing assembly A by directory instead of project. [ ; ) ]
Unfortunately, I am still getting a TypeLoadException...
Update:
Hi, The problem was solved by a colleague at work. The problem was that when moving the type from assembly A to assembly B, you need to keep the type in the same namespace as the type originally had in assembly A. Thanks Roaan