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

  • 相关阅读:
    vue:路由实现原理
    webpack自定义loader和自定义插件
    JS常见的算法
    浅析DES、AES、RSA、MD5加密算法及其应用场景
    关于JSON.parse(JSON.stringify(obj))实现深拷贝应该注意的坑
    Canvas和SVG区别
    解决 webpack 打包文件体积过大
    箭头函数与普通函数的区别
    2.Linux基础命令
    1.Ubuntu系统与vmware虚拟机的安装与使用
  • 原文地址:https://www.cnblogs.com/panjun/p/2782979.html
Copyright © 2011-2022 走看看