CSharp examples for System.Reflection:Assembly
Get all types from assemblies. Types will be cached after first call.
using System.Reflection; using System.Linq; using System.Collections.Generic; using System;/*from w w w .j a v a2 s. c o m*/ public class Main{ /// <summary> /// Get all types from assemblies. /// Types will be cached after first call. /// </summary> /// <returns>Types in assemblies</returns> public static IEnumerable<Type> GetAllTypes() { if (AllTypes.Count != 0) return AllTypes; try { foreach (var assembly in GetAssemblies()) { foreach (var type in assembly.GetTypes().Where(it => it.IsClass || it.IsInterface)) { AllTypes.Add(type); } } return AllTypes; } catch (ReflectionTypeLoadException ex) { AllTypes.Clear(); var first = (ex.LoaderExceptions ?? new Exception[0]).Take(5).ToList(); throw new ApplicationException(string.Format("Can't load types:{0}{1}", Environment.NewLine, string.Join(Environment.NewLine, first.Select(it => it.Message))), ex); } } }