现在的位置: 首页 > 综合 > 正文

根据命名空间和类名创建实例

2013年07月01日 ⁄ 综合 ⁄ 共 3075字 ⁄ 字号 评论关闭

//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();
        }
    }
}

 

【上篇】
【下篇】

抱歉!评论已关闭.