Beispiel #1
0
        private static AssemblyInfoItem GetAssemblyInfo(string fullFileName)
        {
            var assemblyItem = new AssemblyInfoItem
            {
                FullFileName  = fullFileName,
                FileVersion   = new Version(0, 0),
                referenceList = new List <FileItem>()
            };

            AssemblyName[] references = null;
            try
            {
                var ctx = new AssemblyLoadContext(nameof(AssemblyLoader) + "", true);
                var asm = ctx.LoadFromAssemblyPath(fullFileName);
                try
                {
                    assemblyItem.FileVersion = asm.GetName().Version;
                    references = asm.GetReferencedAssemblies();
                }
                catch (Exception e)
                {
                    if (verbose)
                    {
                        Console.WriteLine("Can't get assembly info: " + fullFileName);
                        Console.WriteLine("Exception: " + e.Message);
                    }

                    try
                    {
                        if (verbose)
                        {
                            Console.WriteLine("Re-trying to load assembly: " + fullFileName);
                        }

                        var newAsm = AssemblyName.GetAssemblyName(fullFileName);
                        assemblyItem.FileVersion = newAsm.Version;
                    }
                    catch (Exception e2)
                    {
                        if (verbose)
                        {
                            Console.WriteLine("Still can't load assembly: " + fullFileName);
                            Console.WriteLine("Exception: " + e2.Message);
                        }
                    }
                }
                finally
                {
                    ctx.Unload();
                }

                if (references != null)
                {
                    foreach (var dllReference in references)
                    {
                        assemblyItem.referenceList.Add(new FileItem(dllReference.Name + ".dll", dllReference.Version));
                    }
                }
            }
            catch (Exception e)
            {
                // .NET Core 3.0 do not support ReflectionOnly load

                /*var loadedAssembly = Assembly.ReflectionOnlyLoadFrom(fullFileName);
                 * assemblyItem.fileVersion = loadedAssembly.GetName().Version;
                 * references = loadedAssembly.GetReferencedAssemblies();*/
                if (verbose)
                {
                    Console.WriteLine("Can't load assembly: " + fullFileName);
                    Console.WriteLine("Exception: " + e.Message);
                }

                try
                {
                    if (verbose)
                    {
                        Console.WriteLine("Re-trying to load assembly: " + fullFileName);
                    }

                    var newAsm = AssemblyName.GetAssemblyName(fullFileName);
                    assemblyItem.FileVersion = newAsm.Version;
                }
                catch (Exception e2)
                {
                    if (verbose)
                    {
                        Console.WriteLine("Still can't load assembly: " + fullFileName);
                        Console.WriteLine("Exception: " + e2.Message);
                    }
                }
            }

            return(assemblyItem);
        }
            public AssemblyInfoItem GetAssemblyInfo(string fullFileName)
            {
                var assemblyItem = new AssemblyInfoItem
                {
                    FullFileName  = fullFileName,
                    FileVersion   = new Version(0, 0),
                    ReferenceList = new List <FileItem>()
                };

                AssemblyName[] references = null;
                try
                {
                    try
                    {
                        assemblyLoader.Load(fullFileName);
                    }
                    catch
                    {
                        AppDomain.Unload(_domain);
                        _domain = AppDomain.CreateDomain(nameof(AssemblyLoader),
                                                         AppDomain.CurrentDomain.Evidence,
                                                         new AppDomainSetup
                        {
                            ApplicationBase = Path.GetDirectoryName(typeof(AssemblyLoader).Assembly.Location)
                        });
                        assemblyLoader = (AssemblyLoader)_domain.CreateInstanceAndUnwrap(typeof(AssemblyLoader).Assembly.FullName, typeof(AssemblyLoader).FullName);

                        assemblyLoader.Load(fullFileName);
                    }

                    try
                    {
                        assemblyItem.FileVersion = assemblyLoader.GetVersion();
                        references = assemblyLoader.GetReferences();
                    }
                    catch (Exception e1)
                    {
                        if (_verbose)
                        {
                            Console.WriteLine("Can't get assembly info: " + fullFileName);
                            Console.WriteLine("Exception: " + e1.Message);
                        }

                        try
                        {
                            if (_verbose)
                            {
                                Console.WriteLine("Re-trying to load assembly: " + fullFileName);
                            }

                            var newAsm = AssemblyName.GetAssemblyName(fullFileName);
                            assemblyItem.FileVersion = newAsm.Version;
                        }
                        catch (Exception e2)
                        {
                            if (_verbose)
                            {
                                Console.WriteLine("Still can't load assembly: " + fullFileName);
                                Console.WriteLine("Exception: " + e2.Message);
                            }
                        }
                    }

                    if (references != null)
                    {
                        foreach (var dllReference in references)
                        {
                            assemblyItem.ReferenceList.Add(new FileItem(dllReference.Name + ".dll", dllReference.Version));
                        }
                    }
                }
                catch (Exception e)
                {
                    if (_verbose)
                    {
                        Console.WriteLine("Can't load assembly: " + fullFileName);
                        Console.WriteLine("Exception: " + e.Message);
                    }

                    try
                    {
                        if (_verbose)
                        {
                            Console.WriteLine("Re-trying to load version only: " + fullFileName);
                        }

                        var newAsm = AssemblyName.GetAssemblyName(fullFileName);
                        assemblyItem.FileVersion = newAsm.Version;
                    }
                    catch (Exception e2)
                    {
                        if (_verbose)
                        {
                            Console.WriteLine("Still can't load assembly: " + fullFileName);
                            Console.WriteLine("Exception: " + e2.Message);
                        }
                    }
                }

                return(assemblyItem);
            }