|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.IO; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Reflection; |
| 10 | +using System.Runtime.InteropServices; |
| 11 | +using System.Runtime.Loader; |
| 12 | + |
| 13 | +namespace System |
| 14 | +{ |
| 15 | + internal static class StartupHookProvider |
| 16 | + { |
| 17 | + private const string StartupHookTypeName = "StartupHook"; |
| 18 | + private const string InitializeMethodName = "Initialize"; |
| 19 | + |
| 20 | + // Parse a string specifying a list of assemblies and types |
| 21 | + // containing a startup hook, and call each hook in turn. |
| 22 | + private static void ProcessStartupHooks() |
| 23 | + { |
| 24 | + string startupHooksVariable = (string)AppContext.GetData("STARTUP_HOOKS"); |
| 25 | + if (startupHooksVariable == null) |
| 26 | + { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + // Parse startup hooks variable |
| 31 | + string[] startupHooks = startupHooksVariable.Split(Path.PathSeparator); |
| 32 | + foreach (string startupHook in startupHooks) |
| 33 | + { |
| 34 | + if (String.IsNullOrEmpty(startupHook)) |
| 35 | + { |
| 36 | + throw new ArgumentException(SR.Argument_InvalidStartupHookSyntax); |
| 37 | + } |
| 38 | + if (PathInternal.IsPartiallyQualified(startupHook)) |
| 39 | + { |
| 40 | + throw new ArgumentException(SR.Argument_AbsolutePathRequired); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // Call each hook in turn |
| 45 | + foreach (string startupHook in startupHooks) |
| 46 | + { |
| 47 | + CallStartupHook(startupHook); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Load the specified assembly, and call the specified type's |
| 52 | + // "static void Initialize()" method. |
| 53 | + private static void CallStartupHook(string assemblyPath) |
| 54 | + { |
| 55 | + Debug.Assert(!String.IsNullOrEmpty(assemblyPath)); |
| 56 | + Debug.Assert(!PathInternal.IsPartiallyQualified(assemblyPath)); |
| 57 | + |
| 58 | + Assembly assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath); |
| 59 | + Debug.Assert(assembly != null); |
| 60 | + Type type = assembly.GetType(StartupHookTypeName, throwOnError: true); |
| 61 | + |
| 62 | + // Look for a static method without any parameters |
| 63 | + MethodInfo initializeMethod = type.GetMethod(InitializeMethodName, |
| 64 | + BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, |
| 65 | + null, // use default binder |
| 66 | + Type.EmptyTypes, // parameters |
| 67 | + null); // no parameter modifiers |
| 68 | + |
| 69 | + bool wrongSignature = false; |
| 70 | + if (initializeMethod == null) |
| 71 | + { |
| 72 | + // There weren't any static methods without |
| 73 | + // parameters. Look for any methods with the correct |
| 74 | + // name, to provide precise error handling. |
| 75 | + try |
| 76 | + { |
| 77 | + // This could find zero, one, or multiple methods |
| 78 | + // with the correct name. |
| 79 | + initializeMethod = type.GetMethod(InitializeMethodName, |
| 80 | + BindingFlags.Public | BindingFlags.NonPublic | |
| 81 | + BindingFlags.Static | BindingFlags.Instance); |
| 82 | + } |
| 83 | + catch (AmbiguousMatchException) |
| 84 | + { |
| 85 | + // Found multiple |
| 86 | + Debug.Assert(initializeMethod == null); |
| 87 | + wrongSignature = true; |
| 88 | + } |
| 89 | + if (initializeMethod != null) |
| 90 | + { |
| 91 | + // Found one |
| 92 | + wrongSignature = true; |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + // Didn't find any |
| 97 | + throw new MissingMethodException(StartupHookTypeName, InitializeMethodName); |
| 98 | + } |
| 99 | + } |
| 100 | + else if (initializeMethod.ReturnType != typeof(void)) |
| 101 | + { |
| 102 | + wrongSignature = true; |
| 103 | + } |
| 104 | + |
| 105 | + if (wrongSignature) |
| 106 | + { |
| 107 | + throw new ArgumentException(SR.Format(SR.Argument_InvalidStartupHookSignature, |
| 108 | + StartupHookTypeName + Type.Delimiter + InitializeMethodName, |
| 109 | + assemblyPath)); |
| 110 | + } |
| 111 | + |
| 112 | + Debug.Assert(initializeMethod != null && |
| 113 | + initializeMethod.IsStatic && |
| 114 | + initializeMethod.ReturnType == typeof(void) && |
| 115 | + initializeMethod.GetParameters().Length == 0); |
| 116 | + |
| 117 | + initializeMethod.Invoke(null, null); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments