C# 接口

 Type helloType = typeof(Hello); //Hello是一个接口

            List<Type> types = new List<Type>();
            //遍历程序集
            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                //遍历程序集类型
                foreach (var type in assembly.GetTypes())
                {
                    if (helloType.IsAssignableFrom(type)) //判断类 是否 继承接口了
                    {
                        if (type.IsClass && !type.IsAbstract)
                        {
                            types.Add(type);
                        }
                    }
                }
            }
            //执行继承 接口类的方法
            for (int i = 0; i < types.Count; i++)
            {
                Hello helloInstance = Activator.CreateInstance(types[i]) as Hello;
                helloInstance.Say();
            }
            //lanmuda表达式执行
            types.ForEach(
             (t) =>
            {
                var helloInstance = Activator.CreateInstance(t) as Hello;

                helloInstance.Say();
            });


            Console.ReadKey();
  public interface Hello
        {
            void Say();
        }

        public class A : Hello
        {
            public void Say()
            {
                Console.WriteLine("Say Hello to A");
                Console.ReadLine();
            }
        }

        public class B : Hello
        {
            public void Say()
            {
                Console.WriteLine("Say Hello to B");
                Console.ReadLine();
            }
        }

//接口的作用 是 对已经封装好的 dll 传递对象 改变其执行的方法;   或 把参数 传递给 指定对象下的实现接口的方法

 public interface IMySqlSugar
    {
        List<T> SqlQueryable<T>(string sql);
    }
    //
    public class A : IMySqlSugar
    {
        public List<T> SqlQueryable<T>(string sql)
        {
            throw new System.NotImplementedException();
        }

    }
    public class B
    {
        public List<T> test<T>(string sql)
        {
            SqlSugarHandleHelper shh = new SqlSugarHandleHelper(new A()); //如果用, 最终执行到A类下的方法

            throw new System.NotImplementedException();
        }

    }
原文地址:https://www.cnblogs.com/enych/p/11871506.html