zoukankan      html  css  js  c++  java
  • 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);
            }
    

      

  • 相关阅读:
    JavaScript常用单词整理总结
    花了100多去KTV不是唱歌,竟然是……
    当你左右看看没有发现我时,千万千万别往看……
    winform分页案例简单实现方式~
    来自一位家长的肺腑之言,句句在理!!!
    “小朋友”们节日快乐呀~
    你在学校我安排了你没有做到最多凶你一顿,在公司不一样,直接得让走人!...
    你也可以做一个简易抽奖程序!
    不好意思,你这个加分理由不行……
    【RocketMQ】客户端源码解析
  • 原文地址:https://www.cnblogs.com/Wolfmanlq/p/4556737.html
Copyright © 2011-2022 走看看