Thomas T. Veldhouse <ve*****@yahoo.comwrote:
>
NOTE: this code has not been fully tested ;-) .. not too fond of using
String.Split()
Hmm ... still having trouble! Why doesn't Microsoft honor "Simple" as the
documentation suggests? Anyway, here is the code we have now, but it STILL
doesn't work perfectly with subclasses of generic lists [themself generic]
being serialzied in an object graph where an the list is defined in one
assembly and the contents of the list are in another. Still, here is the code
we have so far, in case anybody google's this up in the future:
sealed class SimpleDeserializationBinder : SerializationBinder
{
private Regex _assemRegex = new Regex("(?<assembly>^.*?),.*");
private Regex _typeRegex = new
Regex("(?<type>.*?),(?<assembly>.*?),.*(?<end>]])");
public override Type BindToType(string assemblyName, string typeName)
{
// remove strong name from assembly
Match match = _assemRegex.Match(assemblyName);
if (match.Success)
{
assemblyName = match.Groups["assembly"].Value;
}
// remove strong name from any generic collections
match = _typeRegex.Match(typeName);
if (match.Success)
{
typeName = string.Format("{0},{1}{2}",
match.Groups["type"].Value,
match.Groups["assembly"].Value,
match.Groups["end"].Value);
}
Type typeToDeserialize = null;
// replace assembly name with the simple assembly name
// - strip the strong name off of the name
string type = string.Format("{0}, {1}", typeName,
assemblyName);
// The following line of code returns the type.
typeToDeserialize = Type.GetType(type);
return typeToDeserialize;
}
}
This code works for nearly all cases I have encountered, except what I
mentioned above, which is likely very rare.
--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1