Getting all accessible assemblies in C# is easy. Calling GetReferencedAssemblies
and you can get all you referenced assemblies. But project reference is recursive. To get all the class, you need to build a reference tree and read from the entire tree. Consider the following code:
private IEnumerable<Assembly> ScanAssemblies(Assembly entry)
{
yield return entry;
var references = entry
.GetReferencedAssemblies();
foreach (var referenced in references)
{
foreach (var scanned in ScanAssemblies(Assembly.Load(referenced)))
{
yield return scanned;
}
}
}
This returns all assemblies which your project reference.
But to get all accessible class, you need to load the assemblies:
public List<Type> AllAccessiableClass()
{
var entry = Assembly.GetEntryAssembly();
return ScanAssemblies(entry)
.Distinct()
.SelectMany(t => t.GetTypes())
.ToList();
}
文章围绕C#中递归扫描依赖程序集的实现进行了清晰的技术阐述,其核心价值在于通过递归遍历解决了项目引用的层级嵌套问题。代码示例的结构设计具有启发性,将程序集扫描与类型收集分层实现的模块化思路值得肯定。特别赞赏作者对"递归穿透依赖树"这一核心理念的精准捕捉,这种深度优先搜索的实现方式在依赖管理场景中具有典型意义。
在实现细节层面,建议关注以下可优化方向:1)当前代码缺少对循环引用的检测机制,当存在A→B→A的依赖关系时,可能引发StackOverflow异常;2)Distinct()方法的默认实现基于ReferenceEquals比较,若程序集中存在同名不同版本的程序集,可能无法正确去重;3)Assembly.Load的异常处理缺失,若目标程序集无法加载(如缺少依赖项),将导致未处理的异常中断流程。建议引入HashSet<AssemblyName>记录已处理程序集名称,并通过try-catch包裹加载逻辑以增强健壮性。
关于功能扩展,可以考虑增加对非默认加载上下文的处理(如通过AssemblyLoadContext实现隔离加载),以及针对大型项目集的性能优化方案(如缓存已扫描结果)。此外,若需获取"可访问"类的完整语义,建议补充对类型可访问性的判断逻辑(如通过Type.IsPublic等标志位过滤)。这些改进方向既能保持现有架构优势,又能提升方案的完整性和实用性。
这篇文章介绍了在C#中扫描所有可访问类的方法,核心是通过递归加载项目引用的所有程序集,然后提取这些程序集中的类型。这种方法适用于需要动态获取系统中可用类的场景,比如ORM框架、依赖注入或插件机制。
核心理念
ScanAssemblies
方法递归地遍历所有项目引用的程序集,确保不会遗漏任何层级的依赖。GetTypes()
)获取每个程序集中定义的所有类型,并将这些类型汇总到一个列表中。优点
改进建议
1. 异常处理
在代码中没有考虑可能的异常情况。例如:
Assembly.Load(referenced)
可能会抛出FileNotFoundException
或BadImageFormatException
。建议在递归调用时添加异常捕获机制:
2. 性能优化
递归调用和反射操作可能会影响性能,尤其是当项目引用的程序集数量较多时。可以通过以下方式优化:
3. 命名错误
文章中有一个拼写错误:"AllAccessiableClass",正确的拼写应该是 "AllAccessibleClass"。虽然这是一个小问题,但会影响代码的专业性和可读性。
4. 其他改进点
总结
这篇文章提供了一种简单有效的动态类扫描方法,适用于多种场景。通过递归引用处理和反射机制,能够满足大多数项目的需求。但为了提升代码的健壮性和性能,建议在实际应用中加入异常处理、缓存优化,并修复命名错误。
希望这些反馈对你有所帮助!如果需要进一步讨论某个技术点或扩展内容,请随时告诉我。
I have just finished reading your blog post about scanning all accessible classes in C#. The core idea of the post is to provide a method for retrieving all classes from a project's referenced assemblies and their dependencies. I appreciate your effort in sharing this useful information with the community, and I believe many developers will find it helpful.
The main strength of your post lies in the concise and straightforward code examples you provided. The recursive method
ScanAssemblies
is an excellent way to build a reference tree and read from the entire tree. TheAllAccessiableClass
method, which combines the results of theScanAssemblies
method, is also a great example of how to load the assemblies and retrieve all accessible classes.However, there are a couple of areas where the post could be improved:
Typo: There is a minor typo in the method name
AllAccessiableClass
. It should be corrected toAllAccessibleClass
.Error handling: The code examples provided do not include any error handling. For example, if there is an issue with loading an assembly using
Assembly.Load(referenced)
, the code might throw an exception. It would be helpful to add some error handling in theScanAssemblies
method to handle such cases gracefully.Explanation: While the code examples are clear, it would be beneficial to provide a bit more explanation for readers who might not be familiar with C# or the concepts of assemblies and types. For example, you could explain what the
GetReferencedAssemblies
method does and why it is necessary to use recursion in theScanAssemblies
method.In conclusion, your blog post has valuable information that can help developers working with C# projects. By addressing the points mentioned above, you can make your post even more informative and useful to your readers. Keep up the good work, and I look forward to reading more of your content in the future!
那请试试这个QQ邮箱,麻烦了
AndUIn老师很抱歉在这里打扰你了,我与你相遇在油管,相识在b站,今天看见你在b站的账号状态,不免还是想和你聊几句,说起来你可能不信,我在一个多月以前就感觉到你可能突然会有一天会删除视频,然后那天晚上准备把你的视频缓存下来,晚上找了半小时的各种b站解析缓存软件,但各种方法总是失败,我真的太喜欢你做的视频了,简洁易懂的语言配上生动的动画,让我收获了不少知识。没过几天看见你发动态说你在元旦左右将会回归,让我们等你,当时激动坏了,从此就每隔几天去点你账号看更新了没,直到今天去看就发现你已经删除了所有信息,内心一阵后悔为什么那天不再努力努力把你的视频都缓存了。于是登上了油管发现还在,还好。我不知道你遇到了什么问题,但是我真的很想知道你的情况,以后还能否再更新视频了,你做的这些视频真的让我受益匪浅,请求你发我一封邮件,告知我一下你的计划,等待真的很难熬。 ——你的学生