Can a call to Assembly.Load(byte[]) raise the AppDomain.AssemblyResolve event?

前端 未结 5 1970
迷失自我
迷失自我 2020-12-25 14:05

Suppose I have a handler for AppDomain.AssemblyResolve event, and in the handler I construct a byte array and invoke the method Assembly.Load(byte[]). Can this method itself

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-25 14:23

    The MSDN Documentation states:

    How the AssemblyResolve Event Works:

    When you register a handler for the AssemblyResolve event, the handler is invoked whenever the runtime fails to bind to an assembly by name. For example, calling the following methods from user code can cause the AssemblyResolve event to be raised:

    1. An AppDomain.Load method overload or Assembly.Load method overload whose first argument is a string that represents the display name of the assembly to load (that is, the string returned by the Assembly.FullName property).

    2. An AppDomain.Load method overload or Assembly.Load method overload whose first argument is an AssemblyName object that identifies the assembly to load.

    It doesn't mention the overload receiving a byte[]. I looked up in the reference source and it seems that the Load which accepts a string overload internally calls a method named InternalLoad, which before invoking the native LoadImage calls CreateAssemblyName and its documentation states:

    Creates AssemblyName. Fills assembly if AssemblyResolve event has been raised.

    internal static AssemblyName CreateAssemblyName(
               String assemblyString, 
               bool forIntrospection, 
               out RuntimeAssembly   assemblyFromResolveEvent)
    {
            if (assemblyString == null)
               throw new ArgumentNullException("assemblyString");
            Contract.EndContractBlock();
    
            if ((assemblyString.Length == 0) ||
                (assemblyString[0] == '\0'))
                throw new ArgumentException(Environment.GetResourceString("Format_StringZeroLength"));
    
            if (forIntrospection)
                AppDomain.CheckReflectionOnlyLoadSupported();
    
            AssemblyName an = new AssemblyName();
    
            an.Name = assemblyString;
            an.nInit(out assemblyFromResolveEvent, forIntrospection, true); // This method may internally invoke AssemblyResolve event.
            
            return an;
    

    The byte[] overload doesn't have this, it simply calls the native nLoadImage inside QCall.dll. This may explain why ResolveEvent isn't invoked.

提交回复
热议问题