最近升级系统里的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); } } } } }