Custom factory class

Time:2019-12-11
using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using System.Reflection;
 using System.Text;
 namespace LYZB.ApiConfig
 {
     /// 
     ///Custom factory class
     /// 
     // generic
     public abstract class AssesUtil where T : class,new()
     {
         /// 
         ///Cache collection
         /// 
         private static Hashtable hash = Hashtable.Synchronized(new Hashtable());
         /// 
         ///Execution instance method
         /// 
         ///Method name
         ///Parameter type
         ///Parameter entity
         /// 
         public static object Invoke(string FunName, Type[] type, params object[] arg)
         {
             object result = null;
             try
             {
                 Type types = typeof(T);
                 var classname = types.Namespace + "." + types.Name;
                 MethodInfo method = types.GetMethod(FunName, type);
                 if (hash[classname] != null)
                 {
                     result = method.Invoke(hash[classname], arg);
                 }
                 else
                 {
                     var func = Activator.CreateInstance(types);
                     result = method.Invoke(func, arg);
                     hash[classname] = func;
                 }
             }
             catch (Exception e)
             {
                 throw e;
             }
             return result;
         }
         /// 
         ///High concurrent execution method
         /// 
         ///Method name
         ///Parameter type
         // / parameter
         /// OBJECT
         public static object InvokeConcurrent(string FunName, Type[] type, params object[] arg)
         {
             object result = null;
             Type types = typeof(T);
             String Key = types.Namespace + "." + types.Name + "." + FunName;
             String parm_key = "";
             try
             {
                 for (int i = 0; i < type.Length; i++)
                 {
                     parm_key += type[i].Name + "_" + arg[i] + "_";
                 }
                 Key = Key +":"+ parm_key.TrimEnd('_');
                 //L2 cache processing
                 if (Cache.GetData(Key) != null)
                 {
                     result = Cache.GetData(Key);
                 }
                 else
                 {
                     Object Odata = Invoke(FunName, type, arg);
                     if (Odata != null)
                     {
                         Cache.Add(Key, Odata, TimeSpan.FromSeconds(1));
                     }
                     result = Odata;
                 }
             }
             catch (Exception ex)
             {
                 throw ex;
             }
             return result;
         }
         /// 
         ///Get single class real column
         /// 
         /// 
         public static T GetInstance()
         {
             T result = default(T);
             try
             {
                 //Get the object type of the generic class
                 Type type = typeof(T);
                 var classname = type.Namespace + "." + type.Name;
                 if (hash[classname] != null)
                 {
                     result = hash[classname] as T;
                 }
                 else
                 {// create an instance of an object based on its type
                     result = Activator.CreateInstance(type) as T;
                     hash[classname] = result;
                 }
             }
             catch (Exception error)
             {
                 throw error;
             }
             return result;
         }
     }
 }