zoukankan      html  css  js  c++  java
  • 根据命名空间和类名创建实例

    //DataAccess.Ioc.cs

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Text;
    using System.Configuration;
    using System.Reflection;

    namespace DataAccess.Ioc
    {
        public class ObjectInstanceFactory
        {
            /// <summary>
            /// 服务实例列表
            /// </summary>
            private static List<object> objects = new List<object>();
            private static List<string> objectsindex = new List<string>();

            /// <summary>
            /// 获取一个指定的实例
            /// </summary>
            /// <param name="assemblyName"></param>
            /// <param name="className"></param>
            /// <returns></returns>
            public static object GetObjectInstance(string assemblyName, string className)
            {
                object objectinstance = null;

                try
                {
                    //从配置文件获取命名空间和类名
                    string configpath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "ConfigIoc.config";
                    assemblyName = ConfigIoc.GetConfigValue(configpath, assemblyName);
                    className = ConfigIoc.GetConfigValue(configpath, className);

                    string fullpath = assemblyName.Trim() + "." + className.Trim();

                    //从服务列表获取实例,如果没有,创建一个实例返回,同时把实例放入服务列表
                    int index = objectsindex.IndexOf(fullpath);
                    if (index >= 0)
                        objectinstance = objects.ToArray().GetValue(index);

                    if (objectinstance != null)
                        return objectinstance;

                    //实例服务列表没有实例,创建一个新实例
                    objectinstance = CreateObjectInstance(assemblyName, className);

                    //新实例加入服务列表
                    if (objectinstance != null)
                    {
                        objects.Add(objectinstance);
                        objectsindex.Add(fullpath);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }

                return objectinstance;
            }

            /// <summary>
            /// 创建一个指定的实例
            /// </summary>
            /// <param name="assemblyName"></param>
            /// <param name="className"></param>
            /// <returns></returns>
            private static object CreateObjectInstance(string assemblyName, string className)
            {
                object objectinstance = null;
                try
                {
                    string fullpath = assemblyName.Trim() + "." + className.Trim();
                    objectinstance = Assembly.Load(assemblyName).CreateInstance(fullpath);
                }
                catch (DllNotFoundException ex)
                {
                    throw new Exception(ex.Message);
                }

                return objectinstance;
            }
        }
    }

    //test.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace TestConsole
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("开始...");
                Console.ReadLine();

                DataAccess.Contract.IDataAccessHelper dataaccess =
                    (DataAccess.Contract.IDataAccessHelper)DataAccess.Ioc.ObjectInstanceFactory.GetObjectInstance("DataAccess.Service", "ServiceHelper");
                string serialNo = dataaccess.GetMaxBillSerialNo("采购订单", "CG");
                Console.WriteLine("单号是:" + serialNo);
                Console.ReadLine();

                Console.ReadLine();
            }
        }
    }

  • 相关阅读:
    [转载]备忘:oh my zsh 的安装、更新、删除
    【转载】fedora22和win10之间的文件共享互访
    python3.7[列表] 索引切片
    注册科创版 等待生效中 测评 投资
    谷歌镜像-20190627
    debian静态地址网络配置方法
    latex高速新手教程
    Java知识点解析
    【Linux 操作系统】Ubuntu 配置 ftp freemind adb
    vs2012设置默认的全局include和lib
  • 原文地址:https://www.cnblogs.com/Kingly/p/1717699.html
Copyright © 2011-2022 走看看