I'm using a SerializationBinder
to bind to the current types in case of changes of type names. Therefore I have a dictionary that binds old type names to the new names.
dict.Add("Old.Namespace.OldTypeName", "New.Namespace.NewName");
I can use this dictionary to get the type in the Type BindToType(string assemblyName, string typeName)
method of the SerializationBinder
. If this type is used in generic types, I must also find the new type of the generic. This answer explained how to do this for List<T>
. I am wondering if there is a standardized format of generic type names, so that I can use some kind of regex to get the new type for all kind of generics.
public class TypeDictionary
{
private Dictionary<string, Type> bindings_ =
new Dictionary<string, Type>();
public void AddBinding(string oldTypeString, Type newType)
{
bindings_.Add(oldTypeString, newType);
}
public Type NewType(string oldTypeString)
{
// How should this be implemented:
if (IsGeneric(oldTypeString))
return NewGenericType(oldTypeString);
Type newType;
if (bindings_.TryGetValue(oldTypeString, out newType))
return newType;
return null;
}
}
Most strings I have seen so far are as List<T>
like this
Namespace.TypeName`1[[MyAssembly.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Is it save to just look for "[[MyAssembly.MyClass,"
to detect a generic Type?