• ReflectionHelper


     public static T GetInstance<T>(Assembly assembly, string fullNamespace)
            {
                return (T)assembly.CreateInstance(fullNamespace);
            }
    
            public static T GetInstance<T>(string assemblyName, string fullNamespace)
            {
                var path = GetFullAssemblyPath(assemblyName);
                return (T)Assembly.LoadFile(path).CreateInstance(fullNamespace);
            }
    
            public static object GetInstance(string assemblyName, string fullNamespace)
            {
                var path = GetFullAssemblyPath(assemblyName);
                return Assembly.LoadFile(path).CreateInstance(fullNamespace);
            }
    
            public static object GetInstance(string typeInfo)
            {
                string[] types = typeInfo.Split(',');
                object tempObject = null;
                if (types.Length == 1)
                {
                    tempObject = GetInstance<Object>(typeof(ReflectionHelper).GetType().Assembly, types[0].Trim());
    
                }
                else if (types.Length == 2)
                {
                    tempObject = GetInstance<Object>(types[1].Trim(), types[0].Trim());
                }
    
                return tempObject;
            }
    
            public static Type GetType(Assembly assembly, string fullNamespace)
            {
                return assembly.CreateInstance(fullNamespace).GetType();
            }
    
            public static Type GetType(string assemblyName, string fullNamespace)
            {
                Assembly assembly = Assembly.LoadFile(GetFullAssemblyPath(assemblyName));
                return GetType(assembly, fullNamespace);
            }
    
            public static Type GetType(string typeInfo)
            {
                string[] types = typeInfo.Split(',');
                Type tempType = null;
                if (types.Length == 1)
                {
                    tempType = GetType(typeof(ReflectionHelper).Assembly, types[0]);
                }
                else if (types.Length == 2)
                {
                    tempType = GetType(types[1].Trim(), types[0].Trim());
                }
    
                return tempType;
            }
    
            public static T GetInstanceFromXml<T>(string typeInfo, string filePath)
            {
                Type configType = GetType(typeInfo);
    
                if (configType == null)
                {
                    throw new Exception(string.Format("occur error when initialize {0}!", filePath));
                }
    
                return XmlHelper.ConvertToObject<T>(configType, filePath);
            }
    
            public static string GetFullAssemblyPath(string assemblyName)
            {
                var path = AppDomain.CurrentDomain.BaseDirectory;
                var debugPath = @"binDebug";
                if (!path.Substring(path.Length - debugPath.Length - 1, debugPath.Length)
                    .ToLower().Equals(debugPath.ToLower()))
                {
                    path = string.Concat(path, @"bin");
                }
    
                return string.Concat(path, assemblyName);
            }
    

      

  • 相关阅读:
    Struts2框架详解
    Eclipse利用Axis2插件构建Web Service并测试
    解决JS中各浏览器Date格式不兼容的问题
    Struts2框架下表单数据的流向以及映射关系
    JMS总结
    第一次博客园
    微信OAuth2网页授权
    将List转换成DataTable
    对文件的读写操作
    Excel文件的导出操作
  • 原文地址:https://www.cnblogs.com/Wolfmanlq/p/4556737.html
Copyright © 2020-2023  润新知