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

  • 相关阅读:
    git stash
    vim 使用
    git 分支管理
    git 日常使用
    js createElement
    git checkout
    2.2链表 链表中倒数第k个结点
    1.8字符串及分析 翻转子串
    全排列
    1.7数组 清除行列
  • 原文地址:https://www.cnblogs.com/panjun/p/2782979.html
Copyright © 2011-2022 走看看