zoukankan      html  css  js  c++  java
  • 接口编程,反射创建对象

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

    namespace ABC.Factory
    {
        public class BLLFactory
        {
            /// <summary>
            /// 创建实例
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="TypeName"></param>
            /// <returns></returns>
            public static T CreateInstant<T>(string TypeName)
            {
                T ret = default(T);
                try
                {
                    string dd = typeof(string).ToString();
                    string FullTypeName = "ABC.BLL." + TypeName;
                    Type type = Type.GetType(FullTypeName);
                    object obj = Activator.CreateInstance(type);
                    ret = (T)obj;
                    return ret;
                }
                catch
                {
                    return ret;
                }
            }

            /// <summary>
            /// 创建实例
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="TypeName"></param>
            /// <param name="Args">构造函数的参数</param>
            /// <returns></returns>
            public static T CreateInstant<T>(string TypeName,object[] Args)
            {
                T ret = default(T);
                try
                {
                    string dd = typeof(string).ToString();
                    string FullTypeName = "ABC.BLL." + TypeName;
                    Type type = Type.GetType(FullTypeName);
                    object obj = Activator.CreateInstance(type, Args);
                    ret = (T)obj;
                    return ret;
                }
                catch
                {
                    return ret;
                }
            }
        }
    }

             //接口编程,反射创建对象
             // 类库IBLL里定义有接口ICXbll ,
            // 类库 BLL里定义有类CXbll 。类FPCXbll实现 接口ICXbll
            // 为使得能够在只引用接口库IBLL而不引用类库 BLL的条件下创建类 BLL.CXbll


             IBLL.ICXbll cxBLL = Factory.BLLFactory.CreateInstant<IBLL.ICXbll>("CXbll");

            BLL.CXbll cxBLL = new ABC.BLL.CXbll();

  • 相关阅读:
    pythonchallenge 解谜 Level 6
    pythonchallenge 解谜 Level 5
    pythonchallenge 解谜 Level 4
    pythonchallenge 解谜 Level 3
    pythonchallenge 解谜 Level 2
    pythonchallenge 解谜 Level 1
    CCF 201912-2 回收站选址
    JavaWeb+JDBC+Servlet+SqlServer实现登录功能
    后台连接数据库的方法
    jQuery实现轮播图
  • 原文地址:https://www.cnblogs.com/panjun/p/2782979.html
Copyright © 2011-2022 走看看