Fix error thrown on multiple non-conforming StartupHook.Initialize signatures (#84158)
authorElinor Fung <elfung@microsoft.com>
Fri, 31 Mar 2023 16:22:12 +0000 (09:22 -0700)
committerGitHub <noreply@github.com>
Fri, 31 Mar 2023 16:22:12 +0000 (09:22 -0700)
src/installer/tests/Assets/TestProjects/StartupHookWithMultipleIncorrectSignatures/StartupHookWithMultipleIncorrectSignatures.cs
src/libraries/System.Private.CoreLib/src/System/StartupHookProvider.cs

index 1fd00c0..79daf76 100644 (file)
@@ -11,11 +11,10 @@ internal class StartupHook
     // case where there are multiple incorrect Initialize
     // methods. Instead, the startup hook provider code should throw
     // an exception.
-    public static int Initialize()
+
+    public void Initialize()
     {
-        Console.WriteLine("Hello from startup hook returning int!");
-        return 10;
+        Console.WriteLine("Hello from startup hook with instance method!");
     }
 
     public static void Initialize(int input)
index c9523b8..78b86b6 100644 (file)
@@ -142,8 +142,6 @@ namespace System
                                                          null, // use default binder
                                                          Type.EmptyTypes, // parameters
                                                          null); // no parameter modifiers
-
-            bool wrongSignature = false;
             if (initializeMethod == null)
             {
                 // There weren't any static methods without
@@ -153,33 +151,24 @@ namespace System
                 {
                     // This could find zero, one, or multiple methods
                     // with the correct name.
-                    initializeMethod = type.GetMethod(InitializeMethodName,
+                    MethodInfo? wrongSigMethod = type.GetMethod(InitializeMethodName,
                                                       BindingFlags.Public | BindingFlags.NonPublic |
                                                       BindingFlags.Static | BindingFlags.Instance);
+                    // Didn't find any
+                    if (wrongSigMethod == null)
+                    {
+                        throw new MissingMethodException(StartupHookTypeName, InitializeMethodName);
+                    }
                 }
                 catch (AmbiguousMatchException)
                 {
                     // Found multiple. Will throw below due to initializeMethod being null.
                     Debug.Assert(initializeMethod == null);
                 }
-
-                if (initializeMethod != null)
-                {
-                    // Found one
-                    wrongSignature = true;
-                }
-                else
-                {
-                    // Didn't find any
-                    throw new MissingMethodException(StartupHookTypeName, InitializeMethodName);
-                }
-            }
-            else if (initializeMethod.ReturnType != typeof(void))
-            {
-                wrongSignature = true;
             }
 
-            if (wrongSignature)
+            // Found Initialize method(s) with non-conforming signatures
+            if (initializeMethod == null || initializeMethod.ReturnType != typeof(void))
             {
                 throw new ArgumentException(SR.Format(SR.Argument_InvalidStartupHookSignature,
                                                       StartupHookTypeName + Type.Delimiter + InitializeMethodName,