Skip to content

Commit

Permalink
Fix error thrown on multiple non-conforming StartupHook.Initialize si…
Browse files Browse the repository at this point in the history
…gnatures (#84158)
  • Loading branch information
elinor-fung authored Mar 31, 2023
1 parent 52bbeeb commit 60b4804
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ private static void CallStartupHook(StartupHookNameOrPath startupHook)
null, // use default binder
Type.EmptyTypes, // parameters
null); // no parameter modifiers

bool wrongSignature = false;
if (initializeMethod == null)
{
// There weren't any static methods without
Expand All @@ -153,33 +151,24 @@ private static void CallStartupHook(StartupHookNameOrPath startupHook)
{
// 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,
Expand Down

0 comments on commit 60b4804

Please sign in to comment.