Skip to content

Commit 60b4804

Browse files
authored
Fix error thrown on multiple non-conforming StartupHook.Initialize signatures (#84158)
1 parent 52bbeeb commit 60b4804

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

src/installer/tests/Assets/TestProjects/StartupHookWithMultipleIncorrectSignatures/StartupHookWithMultipleIncorrectSignatures.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ internal class StartupHook
1111
// case where there are multiple incorrect Initialize
1212
// methods. Instead, the startup hook provider code should throw
1313
// an exception.
14-
15-
public static int Initialize()
14+
15+
public void Initialize()
1616
{
17-
Console.WriteLine("Hello from startup hook returning int!");
18-
return 10;
17+
Console.WriteLine("Hello from startup hook with instance method!");
1918
}
2019

2120
public static void Initialize(int input)

src/libraries/System.Private.CoreLib/src/System/StartupHookProvider.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ private static void CallStartupHook(StartupHookNameOrPath startupHook)
142142
null, // use default binder
143143
Type.EmptyTypes, // parameters
144144
null); // no parameter modifiers
145-
146-
bool wrongSignature = false;
147145
if (initializeMethod == null)
148146
{
149147
// There weren't any static methods without
@@ -153,33 +151,24 @@ private static void CallStartupHook(StartupHookNameOrPath startupHook)
153151
{
154152
// This could find zero, one, or multiple methods
155153
// with the correct name.
156-
initializeMethod = type.GetMethod(InitializeMethodName,
154+
MethodInfo? wrongSigMethod = type.GetMethod(InitializeMethodName,
157155
BindingFlags.Public | BindingFlags.NonPublic |
158156
BindingFlags.Static | BindingFlags.Instance);
157+
// Didn't find any
158+
if (wrongSigMethod == null)
159+
{
160+
throw new MissingMethodException(StartupHookTypeName, InitializeMethodName);
161+
}
159162
}
160163
catch (AmbiguousMatchException)
161164
{
162165
// Found multiple. Will throw below due to initializeMethod being null.
163166
Debug.Assert(initializeMethod == null);
164167
}
165-
166-
if (initializeMethod != null)
167-
{
168-
// Found one
169-
wrongSignature = true;
170-
}
171-
else
172-
{
173-
// Didn't find any
174-
throw new MissingMethodException(StartupHookTypeName, InitializeMethodName);
175-
}
176-
}
177-
else if (initializeMethod.ReturnType != typeof(void))
178-
{
179-
wrongSignature = true;
180168
}
181169

182-
if (wrongSignature)
170+
// Found Initialize method(s) with non-conforming signatures
171+
if (initializeMethod == null || initializeMethod.ReturnType != typeof(void))
183172
{
184173
throw new ArgumentException(SR.Format(SR.Argument_InvalidStartupHookSignature,
185174
StartupHookTypeName + Type.Delimiter + InitializeMethodName,

0 commit comments

Comments
 (0)