zoukankan      html  css  js  c++  java
  • 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();
            }
    
        }
  • 相关阅读:
    「题解」洛谷 P1731 [NOI1999]生日蛋糕
    「题解」洛谷 P1063 能量项链
    Log4j2笔记
    基数排序
    会计知识
    归并排序
    CF668 题解
    拉格朗日反演
    [国家集训队]数颜色 / 维护队列 「带修莫队」
    简单的填数「贪心」
  • 原文地址:https://www.cnblogs.com/enych/p/11871506.html
Copyright © 2011-2022 走看看