public static Assembly[] GetAssemblies(string asmFilename)
{
var result = new List<Assembly>();
var asmPaths = new List<string>();
var stack = new Stack<string>();
stack.Push(asmFilename);
while (stack.Count > 0)
{
string path = stack.Pop();
Assembly asm = null;
try
{
// 检查这个路径
// 看是一个程序名还是一个路径
if ((path.IndexOf(Path.DirectorySeparatorChar, 0, path.Length) != -1) || (path.IndexOf(Path.AltDirectorySeparatorChar, 0, path.Length) != -1))
{
// 从这个路径加载程序集
//asm = Assembly.ReflectionOnlyLoadFrom(path);
asm = Assembly.LoadFrom(path);
}
else
{
// 是一个程序集名称
//asm = Assembly.ReflectionOnlyLoad(path);
asm = Assembly.Load(path);
}
}
catch (Exception)
{
//TODO: add log.
}
// 把程序集添加到列表中
if (asm != null)
{
if (!asmPaths.Contains(path))
{ asmPaths.Add(path); result.Add(asm); }
var referenced = from item in asm.GetReferencedAssemblies() select item;
foreach (AssemblyName item in referenced.Distinct())
{
if (!asmPaths.Contains(item.FullName))
{
stack.Push(item.FullName);
}
}
}
}
return result.ToArray();
}