Get all custom classes in AppDomain.CurrentDomain

I want to iterate over all the classes that I added to my project

Assembly[] foo = AppDomain.CurrentDomain.GetAssemblies(); foreach(Assembly a in foo) { foreach(Type t in a.GetTypes()) { } } 

this is what i tried but i want to exclude the assemblies that are provided by .net like "mscorlib"

+4
source share
3 answers

One common solution is to filter assemblies by name if all of your assemblies have a common prefix (if you have a more or less unique prefix).

 var foo = AppDomain.CurrentDomain.GetAssemblies() .Where(a=>a.FullName.StartsWith("MyProject.")); 

If you are only interested in certain types, consider using attributes for your classes, or even add them at the assembly level.

Example:

Create an attribute:

 [AttributeUsage(AttributeTargets.Assembly)] public class MyAssemblyAttribute : Attribute { } 

Add the following to your AssemblyInfo.cs:

 [assembly: MyAssemblyAttribute()] 

and filter the assembled assemblies:

 var foo = AppDomain.CurrentDomain .GetAssemblies() .Where(a => a.GetCustomAttributes(typeof(MyAssemblyAttribute), false).Any()); 

You will also find this question interesting. One answer suggests checking the full name of each assembly, but it is rather tedious, for example:

 //add more .Net BCL names as necessary var systemNames = new HashSet<string> { "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", "System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" ... }; var isSystemType = systemNames.Contains(objToTest.GetType().Assembly.FullName); 

It is always easier to mark assemblies (by name or attribute) than trying to determine which ones are part of the .Net framework.

+7
source

In one of my projects, I need a list of classes used as business objects. These classes are always user-created types, but can be in a referenced assembly. They do not implement a specific interface and do not exit a specific base class and do not have a distinguishing attribute.

This is the code that I use to filter useful types:

 return type.IsClass && // I need classes !type.IsAbstract && // Must be able to instantiate the class !type.IsNestedPrivate && // Nested private types are not accessible !type.Assembly.GlobalAssemblyCache && // Excludes most of BCL and third-party classes type.Namespace != null && // Yes, it can be null! !type.Namespace.StartsWith("System.") && // EF, for instance, is not in the GAC !type.Namespace.StartsWith("DevExpress.") && // Exclude third party lib !type.Namespace.StartsWith("CySoft.Wff") && // Exclude my own lib !type.Namespace.EndsWith(".Migrations") && // Exclude EF migrations stuff !type.Namespace.EndsWith(".My") && // Excludes types from VB My.something !typeof(Control).IsAssignableFrom(type) && // Excludes Forms and user controls type.GetCustomAttribute<CompilerGeneratedAttribute>() == null && // Excl. compiler gen. !typeof(IControllerBase).IsAssignableFrom(type); // Specific to my project 

Since my user types are not in GAC !type.Assembly.GlobalAssemblyCache , this is pretty good work, excluding most types of BCL libraries (Framework) and some third-party materials.

It is not waterproof but works well. You will most likely need to customize it for your needs.

+1
source

Check the following properties for the type in the inner loop.

 t.GetType().Namespace == "System" t.GetType().Namespace.StartsWith("System") t.GetType().Module.ScopeName == "CommonLanguageRuntimeLibrary" 
0
source

Source: https://habr.com/ru/post/1415955/


All Articles