• 反射工厂


    using System;
    using System.Collections.Generic;
    using System.Reflection;
    
    namespace Beehive.CloudReader.Utility
    {
        /// <summary>
        /// 反射工厂
        /// </summary>
        public abstract class FactoryBase
        {
            /// <summary>
            /// 
            /// </summary>
            protected FactoryBase() { }
    
            /// <summary>
            /// 
            /// </summary>
            protected static readonly SortedList<string, Type> Items = new SortedList<string, Type>();
    
            /// <summary>
            /// 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <returns></returns>
            protected static T CreateType<T>()
            {
                if (Items.ContainsKey(typeof(T).FullName))
                {
                    return (T)Activator.CreateInstance<T>();
                }
                var attributes = typeof(T).GetCustomAttributes(typeof(ConcreteTypeAttribute), true);
                if (attributes.Length > 0 && attributes[0] is ConcreteTypeAttribute)
                {
                    var facadeName = ((ConcreteTypeAttribute)attributes[0]).TypeName;
                    var targetType = Type.GetType(facadeName);
                    lock (Items)
                    {
                        Items[typeof(T).FullName] = targetType;
                    }
                    return (T)Activator.CreateInstance(targetType);
                }
                return default(T);
            }
            /**Type t = typeof(string);
    Type t = typeof(System.String);
    Type 是抽象类, typeof(类名称) 返回的是继承自Type 的RuntimeType*/
    
            /// <summary>
            /// 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="path"></param>
            /// <param name="FullName"></param>
            /// <returns></returns>
            protected static T Create<T>(string path, string FullName)
            {
                return (T)Assembly.Load(path).CreateInstance(FullName);
            }
        }
    }
  • 相关阅读:
    搜狗输入法用户体验评价
    第二阶段团队冲刺5
    第二阶段团队冲刺4
    进度总结报告十四
    第二阶段团队冲刺3
    寻找水王-课上练习
    第二阶段团队冲刺2
    大型网站处理高并发要点技术
    php 处理上百万条的数据库如何提高处理查询速度
    php一次性大量数据入库解决方法
  • 原文地址:https://www.cnblogs.com/wzq806341010/p/3543971.html
Copyright © 2020-2023  润新知