Is it possible to create a binding redirect at run time?

Once the application is running, is there a way to create a binding redirect that will apply to all future assembly downloads?

+5
source share
2 answers

This may be possible with ICLRHostBindingPolicyManager :: ModifyApplicationPolicy, but I have never tried it myself. Note that this is a CLR level interface, so you cannot load policies for individual AppDomains applications (therefore, it is not yet used by PostSharp).

http://msdn.microsoft.com/en-us/library/ms164378.aspx

+1
source

, . , - .

: COM-, ASP-.

http://blog.slaks.net/2013-12-25/redirecting-assembly-loads-at-runtime/

, :

public static void RedirectAssembly(string shortName, Version targetVersion, string publicKeyToken) {
    ResolveEventHandler handler = null;

    handler = (sender, args) => {
        // Use latest strong name & version when trying to load SDK assemblies
        var requestedAssembly = new AssemblyName(args.Name);
        if (requestedAssembly.Name != shortName)
            return null;

        Debug.WriteLine("Redirecting assembly load of " + args.Name
                      + ",\tloaded by " + (args.RequestingAssembly == null ? "(unknown)" : args.RequestingAssembly.FullName));

        requestedAssembly.Version = targetVersion;
        requestedAssembly.SetPublicKeyToken(new AssemblyName("x, PublicKeyToken=" + publicKeyToken).GetPublicKeyToken());
        requestedAssembly.CultureInfo = CultureInfo.InvariantCulture;

        AppDomain.CurrentDomain.AssemblyResolve -= handler;

        return Assembly.Load(requestedAssembly);
    };
    AppDomain.CurrentDomain.AssemblyResolve += handler;
}
+4

All Articles