zoukankan      html  css  js  c++  java
  • 反射工厂

    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);
            }
        }
    }
  • 相关阅读:
    GO 文档笔记
    GO 切片实力踩坑
    关于接口设计的一些反思
    Jenkins 发布.net core 程序,服务端无法下载nuget包的解决方法 error NU1102: 找不到版本为 (>= 3.1.6) 的包
    RabbitMQ 基础概念进阶
    RabbitMQ 入门之基础概念
    Object.entries()使用
    shadow的属性值介绍
    行内元素的特别之处
    margin的特别之处
  • 原文地址:https://www.cnblogs.com/wzq806341010/p/3543971.html
Copyright © 2011-2022 走看看