欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

自定义工厂类

程序员文章站 2023-10-16 19:44:15
1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Reflection; 6 using System.Text; 7 ......
  1 using system;
  2 using system.collections;
  3 using system.collections.generic;
  4 using system.linq;
  5 using system.reflection;
  6 using system.text;
  7 namespace lyzb.apiconfig
  8 {
  9     /// <summary>
 10     /// 自定义工厂类
 11     /// </summary>
 12     /// <typeparam name="t">泛型</typeparam>
 13     public abstract class assesutil<t> where t : class,new()
 14     {
 15         /// <summary>
 16         /// 缓存集合
 17         /// </summary>
 18         private static hashtable hash = hashtable.synchronized(new hashtable());
 19         /// <summary>
 20         /// 执行实例方法
 21         /// </summary>
 22         /// <param name="funname">方法名</param>
 23         /// <param name="type">参数类型</param>
 24         /// <param name="arg">参数实体</param>
 25         /// <returns></returns>
 26         public static object invoke(string funname, type[] type, params object[] arg)
 27         {
 28             object result = null;
 29             try
 30             {
 31                 type types = typeof(t);
 32                 var classname = types.namespace + "." + types.name;
 33                 methodinfo method = types.getmethod(funname, type);
 34                 if (hash[classname] != null)
 35                 {
 36                     result = method.invoke(hash[classname], arg);
 37                 }
 38                 else
 39                 {
 40                     var func = activator.createinstance(types);
 41                     result = method.invoke(func, arg);
 42                     hash[classname] = func;
 43                 }
 44             }
 45             catch (exception e)
 46             {
 47                 throw e;
 48             }
 49             return result;
 50         }
 51         /// <summary>
 52         /// 高并发执行方法
 53         /// </summary>
 54         /// <param name="funname">方法名</param>
 55         /// <param name="type">参数类型</param>
 56         /// <param name="arg">参数</param>
 57         /// <returns>object</returns>
 58         public static object invokeconcurrent(string funname, type[] type, params object[] arg)
 59         {
 60             object result = null;
 61             type types = typeof(t);
 62             string key = types.namespace + "." + types.name + "." + funname;
 63             string parm_key = "";
 64             try
 65             {
 66                 for (int i = 0; i < type.length; i++)
 67                 {
 68                     parm_key += type[i].name + "_" + arg[i] + "_";
 69                 }
 70                 key = key +":"+ parm_key.trimend('_');
 71                 //二级缓存处理
 72                 if (cache.getdata(key) != null)
 73                 {
 74                     result = cache.getdata(key);
 75                 }
 76                 else
 77                 {
 78                     object odata = invoke(funname, type, arg);
 79                     if (odata != null)
 80                     {
 81                         cache.add(key, odata, timespan.fromseconds(1));
 82                     }
 83                     result = odata;
 84                 }
 85             }
 86             catch (exception ex)
 87             {
 88                 throw ex;
 89             }
 90             return result;
 91         }
 92         /// <summary>
 93         /// 获取单个类实列
 94         /// </summary>
 95         /// <returns></returns>
 96         public static t getinstance()
 97         {
 98             t result = default(t);
 99             try
100             {
101                 //获得泛型类的对象类型
102                 type type = typeof(t);
103                 var classname = type.namespace + "." + type.name;
104                 if (hash[classname] != null)
105                 {
106                     result = hash[classname] as t;
107                 }
108                 else
109                 {   //根据对象的类型创建对象的实例
110                     result = activator.createinstance(type) as t;
111                     hash[classname] = result;
112                 }
113             }
114             catch (exception error)
115             {
116                 throw error;
117             }
118             return result;
119         }
120     }
121 }