zoukankan      html  css  js  c++  java
  • 十七.反射技术

    反射:

           常用于工厂,消除switch。

     

           依赖注入(DependencyInjection):解决switch问题。原本需要专门的IoC容器提供,比如Spring.Net。简单的使用.Net技术“反射”既可以。

    格式:

           Assembly.Load(“程序集名称”).CreateInstance(“命名空间.类名称”)

           UsingSystem.Reflection;

    Demo1:

           //常规写法

           IUserresult=new SqlserverUser();

           //反射写法

           UsingSystem.Reflection;

           IUserresult=(IUser)Assembly.Load(“当前程序集的名称”).CreateInstance(“当前命名空间.要实例化的类名”);

    Demo2

           简单工厂

    UsingSystem.Reflection;

    UsingSystem.Configrantion;

           classDataAccess

        {

           private static readonly string AssemblyName = "ConsoleApp2";

           private static readonly string db = "SqlServer";

           //private static readonly string db = "Access";

           //配合App.Config使用

           //private static readonly string db =ConfigurationSettings.AppSettings["DB"];

           public static IUser CreateUser()

           {

               string className = AssemblyName + "." + db + "User";

               return(IUser)Assembly.Load(AssemblyName).CreateInstance(className);

           }

           public static IAdminCreateAdmin()

           {

               IAdmin result = null;

               switch (db)

               {

                    case "SqlServer":

                        result = newSqlServerAdmin();

                        break;

                    case "Access":

                        result = new AccessAdmin();

                       break;

               }

               return result;

           }

  • 相关阅读:
    c--日期和时间函数
    笔试题:360找镇长的题。
    【JavaScript】BOM和DOM
    也谈在 .NET 平台上使用 Scala 语言(续)
    生成n个元素的全排列 C实现
    jsp安全性问题
    stm32DMA通道 ADC通道
    POJ 1860
    Codeforces Round #FF (Div. 2) A. DZY Loves Hash
    Configure the modules to be find by modprobe
  • 原文地址:https://www.cnblogs.com/yaoge/p/1815239.html
Copyright © 2011-2022 走看看