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#中扫描所有可访问类的方法,核心是通过递归加载项目引用的所有程序集,然后提取这些程序集中的类型。这种方法适用于需要动态获取系统中可用类的场景,比如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站解析缓存软件,但各种方法总是失败,我真的太喜欢你做的视频了,简洁易懂的语言配上生动的动画,让我收获了不少知识。没过几天看见你发动态说你在元旦左右将会回归,让我们等你,当时激动坏了,从此就每隔几天去点你账号看更新了没,直到今天去看就发现你已经删除了所有信息,内心一阵后悔为什么那天不再努力努力把你的视频都缓存了。于是登上了油管发现还在,还好。我不知道你遇到了什么问题,但是我真的很想知道你的情况,以后还能否再更新视频了,你做的这些视频真的让我受益匪浅,请求你发我一封邮件,告知我一下你的计划,等待真的很难熬。 ——你的学生