Пример #1
0
        private static byte[] GetOptimizedBytes(string path)
        {
            Environment.CurrentDirectory = Path.GetDirectoryName(path) ?? throw new ArgumentException("Path not found");
            string   asmNameExt      = Path.GetFileName(path);
            string   asmName         = Path.GetFileNameWithoutExtension(asmNameExt);
            string   asmNameMerged   = $"{asmName}.merged{Path.GetExtension(asmNameExt)}";
            string   asmNameMinified = $"{asmName}.minified{Path.GetExtension(asmNameExt)}";
            Assembly asm             = Assembly.LoadFrom(path);
            ILRepack repack          = new ILRepack(new RepackOptions(new[] { "/internalize", $"/out:{asmNameMerged}", asmNameExt }.Concat(GetDependentFilesPass(asm, Environment.CurrentDirectory))));

            repack.Repack();
            ILStrip optimizer = new ILStrip(asmNameMerged);

            optimizer.MakeInternal();
            optimizer.ScanUsedClasses();
            optimizer.ScanUnusedClasses();
            optimizer.CleanupUnusedClasses();
            optimizer.CleanupUnusedResources();
            optimizer.CleanupUnusedReferences();
            optimizer.Save(asmNameMinified);
            optimizer.Dispose();
            File.Delete(asmNameMerged);
            byte[] result = File.ReadAllBytes(asmNameMinified);
            File.Delete(asmNameMinified);
            return(result);
        }