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);
            }
    

      

  • 相关阅读:
    vue 给嵌套的iframe子页面传数据 postMessage
    左边宽度固定,右边宽度自适应的三种写法
    全局变量声明的规范化
    利用__index和__newindex实现默认值表、监控表、只读表
    Metatable和Metamethod
    Lua中的协同程序 coroutine
    Lua中的require
    Lua基础
    D3D的绘制
    效率相关笔记
  • 原文地址:https://www.cnblogs.com/Wolfmanlq/p/4556737.html
Copyright © 2011-2022 走看看