System.Reflection反序

做项目需要用到一些反序

        /// <summary>
        /// 反序对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string ForeachParm(object obj)
        {
            string str = "";
            foreach (System.Reflection.PropertyInfo item in obj.GetType().GetProperties())
            {
                
                str += item.Name + ","+item.GetValue(obj,null)+";";
            }

            return str;
        }

反序接口实现类

 /// <summary>
        /// 反序接口实现类
        /// </summary>
        /// <returns></returns>
        public static IFinanceManage CreateSysManage()
        {
            //dll物理路径
            string AssemblyPath = "U_HomeV3.1Server"; 
            //带命名空间的类全路径
            string classNamespace = "U_HomeV3_1Server.BLL.FinanceManage"; 
            object objType = CreateObject(AssemblyPath, classNamespace);
            
            return (IFinanceManage)objType;        
        }

        private static object CreateObject(string AssemblyPath, string classNamespace)
        {
            object objType = DataCache.GetCache(classNamespace);
            if (objType == null)
            {
                try
                {
                    objType = Assembly.Load(AssemblyPath).CreateInstance(classNamespace);
                    
                }
                catch(Exception ex)
                {
                    //string str=ex.Message;// 记录错误日志
                    return null;
                }
            }
            return objType;
        }
    }

    /// <summary>
    /// 缓存操作类
    /// </summary>
    public class DataCache
    {
        /// <summary>
        /// 获取当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <returns></returns>
        public static object GetCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[CacheKey];
        }

        /// <summary>
        /// 设置当前应用程序指定CacheKey的Cache值
        /// </summary>
        /// <param name="CacheKey"></param>
        /// <param name="objObject"></param>
        public static void SetCache(string CacheKey, object objObject)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject);
        }
    }
原文地址:https://www.cnblogs.com/PLifeCopyDown/p/3016908.html