Exemple #1
0
        MethodJitter(string module, MemberFilter typeFilter, MemberFilter methodFilter, bool runClassConstructors, IEnumerable <string> searchPaths)
        {
            this.typeFilter   = typeFilter;
            this.methodFilter = methodFilter;
            var paths      = new List <string>();
            var modulePath = Path.GetDirectoryName(Path.GetFullPath(module));

            if (modulePath is null)
            {
                throw new ArgumentException(nameof(module));
            }
            paths.Add(modulePath);
            foreach (var path in searchPaths)
            {
                if (Directory.Exists(path))
                {
                    paths.Add(path);
                }
            }
            this.searchPaths = paths.ToArray();
            nameToAssembly   = new Dictionary <string, Assembly?>();
#if NETCOREAPP
            System.Runtime.Loader.AssemblyLoadContext.Default.Resolving += AssemblyLoadContext_Resolving;
#elif NETFRAMEWORK
            AppDomain.CurrentDomain.AssemblyResolve += AppDomain_AssemblyResolve;
#else
#error Unknown target framework
#endif

            var asm      = Assembly.LoadFile(module);
            var allTypes = GetTypes(asm).ToArray();
            if (runClassConstructors)
            {
                foreach (var type in allTypes)
                {
                    if (!(type.TypeInitializer is null))
                    {
                        try {
                            RuntimeHelpers.RunClassConstructor(type.TypeHandle);
                        }
                        catch (Exception ex) {
                            Console.WriteLine($"Failed to run {type.FullName} cctor: {ex.Message}");
                        }
                    }
                }
            }
            foreach (var type in allTypes)
            {
                if (!typeFilter.IsMatch(MakeClrmdTypeName(type.FullName ?? string.Empty), (uint)type.MetadataToken))
                {
                    continue;
                }
                bool isDelegate = typeof(Delegate).IsAssignableFrom(type);
                foreach (var method in GetMethods(type))
                {
                    if (method.IsAbstract)
                    {
                        continue;
                    }
                    if (method.IsGenericMethod)
                    {
                        continue;
                    }
                    if (!methodFilter.IsMatch(method.Name, (uint)method.MetadataToken))
                    {
                        continue;
                    }
#if NETCOREAPP
                    // Not supported on .NET Core
                    if (isDelegate && method is MethodInfo m && m.IsVirtual && (m.Name == "BeginInvoke" || m.Name == "EndInvoke"))
                    {
                        continue;
                    }
#endif
                    try {
                        RuntimeHelpers.PrepareMethod(method.MethodHandle);
                    }
                    catch (Exception ex) {
                        string methodName;
                        try {
                            methodName = method.ToString() ?? "???";
                        }
                        catch {
                            methodName = $"{method.Name} ({method.MetadataToken:X8})";
                        }
                        Console.WriteLine($"{type.FullName}: {methodName}: Failed to jit: {ex.Message}");
                    }
                }
            }
        }