MVC-CZBK.ItcastOA.DALFactory.AbstractFactory-十分重要

using CZBK.ItcastOA.IDAL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CZBK.ItcastOA.DALFactory
{
    /// <summary>
    /// 通过反射的形式创建类的实例
    /// </summary>
   public class AbstractFactory
    {
       private static readonly string AssemblyPath = ConfigurationManager.AppSettings["AssemblyPath"];
       private static readonly string NameSpace = ConfigurationManager.AppSettings["NameSpace"];
       public static IUserInfoDal CreateUserInfoDal()
       {
           string fullClassName = NameSpace + ".UserInfoDal";
          return CreateInstance(fullClassName) as IUserInfoDal;
       }
       private static object CreateInstance(string className)
       {
          var assembly= Assembly.Load(AssemblyPath);
          return assembly.CreateInstance(className);
       }
    }
}

 <appSettings>
    <!--配置程序集名称与命名空间的名称-->
    <add key="AssemblyPath" value="CZBK.ItcastOA.DAL"/>
    <add key="NameSpace" value="CZBK.ItcastOA.DAL"/>
  </appSettings>

猜你喜欢

转载自blog.csdn.net/dxm809/article/details/87514830
今日推荐