CSharp examples for System.Reflection:Assembly
Retrieve a list of Types from an external assembly, implementing a given T type (class or interface).
using System.Reflection; using System.Linq; using System.IO;//w w w.ja va2 s .co m using System.Collections.Generic; using System; public class Main{ /// <summary> /// Retrieve a list of <c>Type</c>s from an external assembly, implementing a given <c>T</c> type (class or interface). /// </summary> /// <typeparam name="T">Type to be searched.</typeparam> /// <param name="assemblyFileName">Path and filename to the assembly.</param> /// <returns>A list of <c>Type</c> implementing <typeparamref name="T"/>.</returns> public static List<Type> GetTypesByInterface<T>(string assemblyFileName) { if (assemblyFileName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) { assemblyFileName = Path.GetFileNameWithoutExtension(assemblyFileName); } var assembly = Assembly.Load(new AssemblyName(assemblyFileName)); var typeInfo = typeof(T).GetTypeInfo(); return assembly.ExportedTypes .Where(t => t.GetTypeInfo().IsAssignableFrom(typeInfo)) .ToList(); } }