惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AI
AI
SecWiki News
SecWiki News
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Engineering at Meta
Engineering at Meta
博客园 - 叶小钗
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
N
News and Events Feed by Topic
Cloudbric
Cloudbric
B
Blog
Cisco Talos Blog
Cisco Talos Blog
V
Vulnerabilities – Threatpost
N
News and Events Feed by Topic
V
Visual Studio Blog
A
Arctic Wolf
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
U
Unit 42
S
Security @ Cisco Blogs
博客园 - 聂微东
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
Y
Y Combinator Blog
G
GRAHAM CLULEY
L
LINUX DO - 热门话题
量子位
NISL@THU
NISL@THU
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Troy Hunt's Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tenable Blog
月光博客
月光博客
S
Security Affairs
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Hacker News
The Hacker News
Spread Privacy
Spread Privacy
D
Docker
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
博客园 - 司徒正美
T
The Exploit Database - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
Help Net Security
Help Net Security
D
DataBreaches.Net

博客园 - 随风逝去(叶进)

MySQL分表(Partition)学习研究报告 用WPF+MongoDB开发房产信息收集器(4)——房产信息采集器总体介绍附程序下载 用WPF+MongoDB开发房产信息收集器(3)——MongoDB入门 用WPF+MongoDB开发房产信息收集器(2)——后台线程 用WPF+MongoDB开发房产信息收集器(1) 解决FireFox不能Debug Silverlight程序的问题 - 随风逝去(叶进) - 博客园 终于可以继续往下了! 回首2008 另类的二级域名实现方法 【更新 2008.10.16】触发C#Button的双击事件 触发C#Button的双击事件 到底要不要去面试? 【C#食谱】【面食】菜单7:用默认值初始化泛型变量 类和对象 【C#食谱】【杭帮菜】菜单2:写一个TCP客户端 C#Winform下用正则表达式限制TextBox只能输入数字 【C#食谱】【杭帮菜】菜单1:写一个TCP服务端 实现需求工程的成功方法 实现需求工程的成功方法——难度:高;影响:低
【C#食谱】【川菜】菜单1:列出被引用的程序集
随风逝去(叶进) · 2008-04-18 · via 博客园 - 随风逝去(叶进)

问题:

你需要得到被一个特定的程序集所引用的所有程序集。这个信息可以告诉你这个程序集是否在引用一个或多个你所创建的程序集,或者你的程序集是否在引用其他特定的程序集。

解决方法:

使用Assembly.GetReferencedAssemblies方法去得到一个程序集所引用的程序集。例如:

public static string[] BuildDependentAssemblyList(string path, List<string> assemblies)
{
// 维护一个本程序集需要的程序集列表
if (assemblies == null)
assemblies = new List<string>();

// 是否已经包含这个路径的程序了
if (assemblies.Contains(path) == true)
return (new string[0]);
Assembly asm = null;
// 检查这个路径
// 看是一个程序名还是一个路径
if ((path.IndexOf(Path.DirectorySeparatorChar, 0, path.Length) != -1) ||
(path.IndexOf(Path.AltDirectorySeparatorChar, 0, path.Length) != -1))
{
// 从这个路径加载程序集
asm = Assembly.ReflectionOnlyLoadFrom(path);
}
else
{
// 是一个程序集名称
asm = Assembly.ReflectionOnlyLoad(path);
}

// 把程序集添加到列表中
if (asm != null)
{
assemblies.Add(path);
}
// 获取所引用的程序集
AssemblyName[] imports = asm.GetReferencedAssemblies();

// 遍历所有的引用,并进行递归
foreach (AssemblyName asmName in imports)
{
BuildDependentAssemblyList(asmName.FullName, assemblies);
}

string[] temp = new string[assemblies.Count];
assemblies.CopyTo(temp, 0);
return (temp);
}

这段代码返回一个包含有原程序集、所有其引用的程序集和这些程序集依赖的其他程序集的string数组。

举个例子:

对D:\WriteName\bin\Debug\WriteName.exe运行这个方法,将会得到以下结果:

D:\WriteName\bin\Debug\WriteName.exe

mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

其中WriteName的代码是这样的:

using System;
using System.Collections.Generic;
using System.Text;

namespace WriteName
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Welcome to http://adaiye.cnblogs.com");
}
}
}

讨论:

获取一个程序集所引用的类型在检查哪些程序集正在被另一个程序集所引用是非常有用的。这个认识可以给予学习如何使用一个新的程序集很大的帮助。这个方法同时可以帮助检查程序集之间的依赖关系。

System.Reflection.Assembly类的GetreferencedAssemblies方法可以获取一个所有引用的程序集的列表。这个方法没有参数,返回一个AssemblyName对象的数组,而不是Types的数组。AssemblyName类型包含的成员包括程序集的名字,版本,本地信息(culture information),公钥/私钥对和其他信息。

注意,这个方法并不计算使用Assembly.ReflectionOnlyLoad*方法加载的程序集,因为,这些程序集只有在编译时才会被检查。当用反射为检查而加载程序集时,你应该使用ReflectionOnlyLoad*方法。这些方法不允许你从已加载的程序集中执行代码。