Find Interfaces In Assembly - CSharp System.Reflection

CSharp examples for System.Reflection:Assembly

Description

Find Interfaces In Assembly

Demo Code


using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;/*from   w  w w. j  a v a  2s.c  o  m*/

public class Main{

        public static IDictionary<Type, Type> FindInterfacesInAssembly<T>(this Assembly assembly, BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.NonPublic) where T : class
        {
            IDictionary<Type, Type> datas = new Dictionary<Type, Type>();

            var classTypes = assembly.GetTypes();
            var returnType = typeof(T).Name;
            foreach (var item in classTypes)
            {
                var interfaces = item.GetInterfaces();
                if (interfaces.Count() > 0)
                {
                    var interfaceOfT = interfaces.FirstOrDefault(i => i.Name.Equals(returnType));
                    if (interfaceOfT != null)
                    {
                        datas.Add(item, interfaceOfT);
                    }
                }
            }
            return datas;
        }
}

Related Tutorials