• 扫描.net dll引用dll


     最近升级系统里的NHibernate,从3.3到4,项目工程太多, 一个模块分bll,dal,model,web,test,10几个模块,就要60多dll,升级一次太头疼。

    编译过后,有时候会有的dll没引用到对的NHibernate,但是又不说具体哪个dll,手工一个个找也挺麻烦的。后来看了下资料可以用反射,

    Assembly.GetReferencedAssemblies()来看下引用的dll。

    /*
     * User: Peter yu
     * Date: 2015/1/7
     * Time: 10:50
     * 
     */
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Reflection;
    
    namespace SearchBin
    {
        
        
        public class RefAssemblyInfo
        {
            public AssemblyName AssemblyName
            {
                get;
                set;
            }
            public Assembly SourceAssembly
            {
                get;
                set;
            }
            
                
        }
        class Program
        {
            public static void Main(string[] args)
            {
            
                List<RefAssemblyInfo> infos = new List<RefAssemblyInfo>();
                    
                LoadAssembly( assembly =>
                             {
                                 
                                 foreach(var name in assembly.GetReferencedAssemblies())
                                 {
                                     RefAssemblyInfo refAssemblyInfo = new RefAssemblyInfo();
                                     
                                     refAssemblyInfo.AssemblyName  = name;
                                     refAssemblyInfo.SourceAssembly= assembly;
                                     infos.Add(refAssemblyInfo);
                                 }
                         
                                 
                             },
                             "*.*.dll",
                             @"D:projectsehr10_new_2Webin");
                
                var resultList= infos
                    .Where (r=>r.AssemblyName.Name =="NHibernate" && r.AssemblyName.Version.Major <4
                            || r.AssemblyName.Name =="FluentNHibernate" && r.AssemblyName.Version.Major <2
                           )
                    
                    .OrderBy(r=>r.AssemblyName.Name);
                
                foreach(var item in resultList)
                {
                
                    Console.WriteLine("{0},{1}",item.AssemblyName.FullName,item.SourceAssembly.FullName);
                        
                }
                Console.ReadLine();
                
            }
            
            public static void LoadAssembly(
                Action<Assembly> load,
                string pattern ="*.*",
                string path = "",
                Func<string, bool> except = null,
                bool shadow = false)
            {
    
                string adjustPath = path;
                
              
    
                DirectoryInfo dirInfo = new DirectoryInfo(adjustPath);
                foreach (FileInfo file in dirInfo.GetFiles(pattern))
                {
                    try{
                    bool isFilePass = true;
                    if (except != null)
                    {
                        isFilePass = !except(file.FullName);
    
                    }
                  
                    if (isFilePass)
                    {
                        Assembly assembly;
                        if (!shadow)
                        {
                            assembly = Assembly.LoadFile(file.FullName);
                        }
                        else
                        {
                            string dllName = file.Name.Remove(file.Name.LastIndexOf("."));
                            assembly = Assembly.Load(dllName);
                        }
                        load(assembly);
                    }
                    }catch(Exception error)
                    {
                        Console.WriteLine(error);
                    }
                }
            }
        }
    }
  • 相关阅读:
    PDF太大怎么办?缩小PDF的4种常用方法
    电脑插上网线无法连接网络完美解决方案
    HTTP攻击与防范-跨站攻击-01简介
    HTTP攻击与防范-跨网站脚本攻击
    HTTP攻击与防范-PHP客户端脚本攻击
    HTTP攻击与防护-函数注入攻击
    HTTP攻击与防范-命令注入攻击
    重置NSX防火墙策略
    NBU8.1安装
    Vcenter由Win2008r2迁移到linux
  • 原文地址:https://www.cnblogs.com/peteryu007/p/4213226.html
Copyright © 2020-2023  润新知