Get all types in the Assembly that implement the target TInterface - CSharp System.Reflection

CSharp examples for System.Reflection:Assembly

Description

Get all types in the Assembly that implement the target TInterface

Demo Code


using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;//from www.  jav a2s. co  m
using Autofac;

public class Main{
        /// <summary>
        /// Get all types in the Assembly that implement the target TInterface
        /// </summary>
        /// <param name="assemblyName">Name of the target assembly</param>
        public static IList<Type> GetImplementFromAssembly<TInterface>(string assemblyName)
        {
            // Load the assembly
            Assembly assembly = Assembly.Load(new AssemblyName(assemblyName));
            // Get list of types that assignable to TInterface and not equal to TInterface
            return assembly.GetTypes()
                .Where(t => t != typeof(TInterface) && t.IsAssignableTo<TInterface>())
                .ToList();
        }
}

Related Tutorials