What is the best way to deserialize generics written with a different version of a signed assembly?

In other cases, it was suggested to simply add a SerializationBinder, which removes the version from the assembly type. However, when using general collections of the type found in the signed assembly, this type is strictly versioned based on its assembly.

Here is what I found.

internal class WeaklyNamedAppDomainAssemblyBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        ResolveEventHandler handler = new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        AppDomain.CurrentDomain.AssemblyResolve += handler;

        Type returnedType;
        try
        {
            AssemblyName asmName = new AssemblyName(assemblyName);
            var assembly = Assembly.Load(asmName);
            returnedType = assembly.GetType(typeName);
        }
        catch
        {
            returnedType = null;
        }
        finally
        {
            AppDomain.CurrentDomain.AssemblyResolve -= handler;
        }

        return returnedType;
    }

    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        string truncatedAssemblyName = args.Name.Split(',')[0];
        Assembly assembly = Assembly.Load(truncatedAssemblyName);
        return assembly;
    }
}

However, changing the binding process around the world seems pretty dangerous to me. Strange things can happen if serialization occurs in multiple threads. Perhaps the best solution is to manipulate some regular expression of type Name?

Edit: The row-based method does not work. Obviously generics require a full, strongly named type. Pretty disgusting if you ask me.

+3
3

AssemblyResolve . , , , . , .

AssemblyResolve, , . .

+2

, , ? SerilizationBinder.

0

This should answer your question: SerializationBinder with <T> list

When using generic types in SerializationBinder.BindToType you need to use weak type names instead of full type names.

0
source

Source: https://habr.com/ru/post/1739859/


All Articles