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