123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- namespace SqlSugar
- {
- public class InstanceFactory
- {
- static Assembly assembly = Assembly.GetExecutingAssembly();
- static Dictionary<string, Type> typeCache = new Dictionary<string, Type>();
- private static string _CustomDllName = "";
- private static List<string> CustomDlls = new List<string>();
- public static Assembly[] CustomAssemblies = new Assembly[]{};
- public static string CustomDllName {
- get { return _CustomDllName; }
- set
- {
- if (!CustomDlls.Contains(value))
- {
- CustomDlls.Add(value);
- }
- _CustomDllName = value;
- }
- }
- public static string CustomDbName = "";
- public static string CustomNamespace = "";
- public static bool NoCache = false;
- public static bool IsWebFrom = false;
- public static void RemoveCache()
- {
- typeCache = new Dictionary<string, Type>();
- }
- #region Queryable
- public static ISugarQueryable<T> GetQueryable<T>(ConnectionConfig currentConnectionConfig)
- {
- if (currentConnectionConfig.DbType == DbType.SqlServer)
- {
- return new SqlServerQueryable<T>();
- }
- else if (currentConnectionConfig.DbType == DbType.MySql)
- {
- return new MySqlQueryable<T>();
- }
- else if (currentConnectionConfig.DbType == DbType.PostgreSQL)
- {
- return new PostgreSQLQueryable<T>();
- }
- else
- {
- string className = "Queryable";
- className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
- ISugarQueryable<T> result = CreateInstance<T, ISugarQueryable<T>>(className);
- return result;
- }
- }
- public static ISugarQueryable<T, T2> GetQueryable<T, T2>(ConnectionConfig currentConnectionConfig)
- {
- if (currentConnectionConfig.DbType == DbType.SqlServer)
- {
- return new SqlServerQueryable<T, T2>();
- }
- else
- {
- string className = "Queryable";
- className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
- ISugarQueryable<T, T2> result = CreateInstance<T, T2, ISugarQueryable<T, T2>>(className);
- return result;
- }
- }
- public static ISugarQueryable<T, T2, T3> GetQueryable<T, T2, T3>(ConnectionConfig currentConnectionConfig)
- {
- if (currentConnectionConfig.DbType == DbType.SqlServer)
- {
- return new SqlServerQueryable<T, T2, T3>();
- }
- else
- {
- string className = "Queryable";
- className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
- ISugarQueryable<T, T2, T3> result = CreateInstance<T, T2, T3, ISugarQueryable<T, T2, T3>>(className);
- return result;
- }
- }
- public static ISugarQueryable<T, T2, T3, T4> GetQueryable<T, T2, T3, T4>(ConnectionConfig currentConnectionConfig)
- {
- string className = "Queryable";
- className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
- ISugarQueryable<T, T2, T3, T4> result = CreateInstance<T, T2, T3, T4, ISugarQueryable<T, T2, T3, T4>>(className);
- return result;
- }
- public static ISugarQueryable<T, T2, T3, T4, T5> GetQueryable<T, T2, T3, T4, T5>(ConnectionConfig currentConnectionConfig)
- {
- string className = "Queryable";
- className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
- ISugarQueryable<T, T2, T3, T4, T5> result = CreateInstance<T, T2, T3, T4, T5, ISugarQueryable<T, T2, T3, T4, T5>>(className);
- return result;
- }
- public static ISugarQueryable<T, T2, T3, T4, T5, T6> GetQueryable<T, T2, T3, T4, T5, T6>(ConnectionConfig currentConnectionConfig)
- {
- string className = "Queryable";
- className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
- ISugarQueryable<T, T2, T3, T4, T5, T6> result = CreateInstance<T, T2, T3, T4, T5, T6, ISugarQueryable<T, T2, T3, T4, T5, T6>>(className);
- return result;
- }
- public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7> GetQueryable<T, T2, T3, T4, T5, T6, T7>(ConnectionConfig currentConnectionConfig)
- {
- string className = "Queryable";
- className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
- ISugarQueryable<T, T2, T3, T4, T5, T6, T7> result = CreateInstance<T, T2, T3, T4, T5, T6, T7, ISugarQueryable<T, T2, T3, T4, T5, T6, T7>>(className);
- return result;
- }
- public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8>(ConnectionConfig currentConnectionConfig)
- {
- string className = "Queryable";
- className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
- ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> result = CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8>>(className);
- return result;
- }
- #region 9-12
- public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9>(ConnectionConfig currentConnectionConfig)
- {
- string className = "Queryable";
- className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
- ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9> result = CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9>>(className);
- return result;
- }
- public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>(ConnectionConfig currentConnectionConfig)
- {
- string className = "Queryable";
- className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
- ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10> result = CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10>>(className);
- return result;
- }
- public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(ConnectionConfig currentConnectionConfig)
- {
- string className = "Queryable";
- className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
- ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> result = CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>>(className);
- return result;
- }
- public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(ConnectionConfig currentConnectionConfig)
- {
- string className = "Queryable";
- className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
- ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> result = CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>>(className);
- return result;
- }
- #endregion
- #endregion
- public static QueryBuilder GetQueryBuilderWithContext(ISqlSugarClient db)
- {
- if (db is SqlSugarClient)
- {
- db = (db as SqlSugarClient).Context;
- }
- else if (db is SqlSugarScope)
- {
- db = (db as SqlSugarScope).ScopedContext.Context;
- }
- if (!(db is SqlSugarProvider))
- {
- db = new SqlSugarClient(db.CurrentConnectionConfig).Context;
- }
- var QueryBuilder = InstanceFactory.GetQueryBuilder(db.CurrentConnectionConfig);
- QueryBuilder.Context = (SqlSugarProvider)db;
- QueryBuilder.Builder = InstanceFactory.GetSqlbuilder(db.CurrentConnectionConfig);
- QueryBuilder.Builder.Context = (SqlSugarProvider)db;
- return QueryBuilder;
- }
- public static QueryBuilder GetQueryBuilder(ConnectionConfig currentConnectionConfig)
- {
- if (currentConnectionConfig.DbType == DbType.SqlServer)
- {
- return new SqlServerQueryBuilder();
- }
- else if (currentConnectionConfig.DbType == DbType.MySql)
- {
- return new MySqlQueryBuilder();
- }
- else
- {
- QueryBuilder result = CreateInstance<QueryBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), "QueryBuilder"));
- return result;
- }
- }
- public static InsertBuilder GetInsertBuilder(ConnectionConfig currentConnectionConfig)
- {
- InsertBuilder result = CreateInstance<InsertBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), "InsertBuilder"));
- return result;
- }
- public static UpdateBuilder GetUpdateBuilder(ConnectionConfig currentConnectionConfig)
- {
- UpdateBuilder result = CreateInstance<UpdateBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), "UpdateBuilder"));
- return result;
- }
- public static DeleteBuilder GetDeleteBuilder(ConnectionConfig currentConnectionConfig)
- {
- DeleteBuilder result = CreateInstance<DeleteBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), "DeleteBuilder"));
- return result;
- }
- public static ILambdaExpressions GetLambdaExpressions(ConnectionConfig currentConnectionConfig)
- {
- if (currentConnectionConfig.DbType == DbType.SqlServer)
- {
- return new SqlServerExpressionContext();
- }
- else if (currentConnectionConfig.DbType == DbType.MySql)
- {
- return new MySqlExpressionContext();
- }
- else
- {
- ILambdaExpressions result = CreateInstance<ILambdaExpressions>(GetClassName(currentConnectionConfig.DbType.ToString(), "ExpressionContext"));
- return result;
- }
- }
- public static ISqlBuilder GetSqlbuilder(ConnectionConfig currentConnectionConfig)
- {
- if (currentConnectionConfig.DbType == DbType.SqlServer)
- {
- return new SqlServerBuilder();
- }
- else if (currentConnectionConfig.DbType == DbType.MySql)
- {
- return new MySqlBuilder();
- }
- else
- {
- ISqlBuilder result = CreateInstance<ISqlBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), "Builder"));
- return result;
- }
- }
- public static UpdateableProvider<T> GetUpdateableProvider<T>(ConnectionConfig currentConnectionConfig) where T : class, new()
- {
- if (currentConnectionConfig.DbType == DbType.Oracle)
- {
- return new OracleUpdateable<T>();
- }
- else
- {
- return new UpdateableProvider<T>();
- }
- }
- public static DeleteableProvider<T> GetDeleteableProvider<T>(ConnectionConfig currentConnectionConfig) where T : class, new()
- {
- if (currentConnectionConfig.DbType == DbType.Oracle)
- {
- return new OracleDeleteable<T>();
- }
- else
- {
- return new DeleteableProvider<T>();
- }
- }
- public static InsertableProvider<T> GetInsertableProvider<T>(ConnectionConfig currentConnectionConfig) where T : class, new()
- {
- if (currentConnectionConfig.DbType == DbType.Oracle)
- {
- return new OracleInsertable<T>();
- }
- else if (currentConnectionConfig.DbType == DbType.PostgreSQL)
- {
- return new PostgreSQLInserttable<T>();
- }
- else if (currentConnectionConfig.DbType == DbType.Kdbndp)
- {
- return new KdbndpInserttable<T>();
- }
- else if (currentConnectionConfig.DbType == DbType.Oscar)
- {
- return new KdbndpInserttable<T>();
- }
- else if (IsCustomDb(currentConnectionConfig))
- {
- var name =
- "SqlSugar." + currentConnectionConfig.DbType +
- "." + currentConnectionConfig.DbType
- + "Insertable`1";
- var type = GetCustomTypeByClass<T>(name);
- if (type == null)
- {
- return new InsertableProvider<T>();
- }
- else
- {
- return (InsertableProvider<T>)Activator.CreateInstance(type, true);
- }
- }
- else
- {
- return new InsertableProvider<T>();
- }
- }
- private static bool IsCustomDb(ConnectionConfig currentConnectionConfig)
- {
- return
- currentConnectionConfig.DbType != DbType.SqlServer &&
- currentConnectionConfig.DbType != DbType.Dm &&
- currentConnectionConfig.DbType != DbType.Oscar &&
- currentConnectionConfig.DbType != DbType.Access &&
- currentConnectionConfig.DbType != DbType.QuestDB &&
- currentConnectionConfig.DbType != DbType.MySql &&
- currentConnectionConfig.DbType != DbType.Oracle &&
- currentConnectionConfig.DbType != DbType.PostgreSQL &&
- currentConnectionConfig.DbType != DbType.ClickHouse &&
- currentConnectionConfig.DbType != DbType.GBase &&
- currentConnectionConfig.DbType != DbType.Sqlite &&
- GetCustomTypeByClass("SqlSugar." + currentConnectionConfig.DbType + "." + currentConnectionConfig.DbType + "Provider") != null;
- }
- public static IDbBind GetDbBind(ConnectionConfig currentConnectionConfig)
- {
- if (currentConnectionConfig.DbType == DbType.SqlServer)
- {
- return new SqlServerDbBind();
- }
- else if (currentConnectionConfig.DbType == DbType.MySql)
- {
- return new MySqlDbBind();
- }
- else
- {
- IDbBind result = CreateInstance<IDbBind>(GetClassName(currentConnectionConfig.DbType.ToString(), "DbBind"));
- return result;
- }
- }
- public static IDbMaintenance GetDbMaintenance(ConnectionConfig currentConnectionConfig)
- {
- IDbMaintenance result = CreateInstance<IDbMaintenance>(GetClassName(currentConnectionConfig.DbType.ToString(), "DbMaintenance"));
- return result;
- }
- public static IDbFirst GetDbFirst(ConnectionConfig currentConnectionConfig)
- {
- IDbFirst result = CreateInstance<IDbFirst>(GetClassName(currentConnectionConfig.DbType.ToString(), "DbFirst"));
- return result;
- }
- public static ICodeFirst GetCodeFirst(ConnectionConfig currentConnectionConfig)
- {
- ICodeFirst result = CreateInstance<ICodeFirst>(GetClassName(currentConnectionConfig.DbType.ToString(), "CodeFirst"));
- return result;
- }
- public static IAdo GetAdo(ConnectionConfig currentConnectionConfig)
- {
- if (currentConnectionConfig.DbType == DbType.SqlServer)
- {
- return new SqlServerProvider();
- }
- else
- {
- IAdo result = CreateInstance<IAdo>(GetClassName(currentConnectionConfig.DbType.ToString(), "Provider"));
- return result;
- }
- }
- private static string GetClassName(string type, string name)
- {
- if (type == "MySqlConnector")
- {
- return "SqlSugar.MySqlConnector.MySql" + name;
- }
- else if (type == "Access")
- {
- return "SqlSugar.Access.Access" + name;
- }
- else if (type == "ClickHouse")
- {
- return "SqlSugar.ClickHouse.ClickHouse" + name;
- }
- else if (type == "GBase")
- {
- return "SqlSugar.GBase.GBase" + name;
- }
- else if (type == "Odbc")
- {
- return "SqlSugar.Odbc.Odbc" + name;
- }
- else if (type == "Custom")
- {
- return CustomNamespace + "."+CustomDbName + name;
- }
- else
- {
- //if (!string.IsNullOrEmpty(CustomDllName))
- //{
- // type = CustomDllName;
- //}
- return UtilConstants.AssemblyName + "." + type + name;
- }
- }
- #region CreateInstance
- private static Restult CreateInstance<T, Restult>(string className)
- {
- return CreateInstance<Restult>(className, typeof(T));
- }
- private static Restult CreateInstance<T, T2, Restult>(string className)
- {
- return CreateInstance<Restult>(className, typeof(T), typeof(T2));
- }
- private static Restult CreateInstance<T, T2, T3, Restult>(string className)
- {
- return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3));
- }
- private static Restult CreateInstance<T, T2, T3, T4, Restult>(string className)
- {
- return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4));
- }
- private static Restult CreateInstance<T, T2, T3, T4, T5, Restult>(string className)
- {
- return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5));
- }
- private static Restult CreateInstance<T, T2, T3, T4, T5, T6, Restult>(string className)
- {
- return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6));
- }
- private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, Restult>(string className)
- {
- return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7));
- }
- private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, Restult>(string className)
- {
- return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8));
- }
- #region 9-12
- private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, Restult>(string className)
- {
- return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9));
- }
- private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, Restult>(string className)
- {
- return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10));
- }
- private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Restult>(string className)
- {
- return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11));
- }
- private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Restult>(string className)
- {
- return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10), typeof(T11), typeof(T12));
- }
- #endregion
- private static Restult CreateInstance<Restult>(string className, params Type[] types)
- {
- try
- {
- if (NoCache)
- {
- return NoCacheGetCacheInstance<Restult>(className, types);
- }
- else
- {
- return GetCacheInstance<Restult>(className, types);
- }
- }
- catch
- {
- NoCache = true;
- return NoCacheGetCacheInstance<Restult>(className, types);
- }
- }
- private static Restult GetCacheInstance<Restult>(string className, Type[] types)
- {
- var cacheKey = className + string.Join(",", types.Select(it => it.FullName));
- Type type=null;
- if (typeCache.ContainsKey(cacheKey))
- {
- type = typeCache[cacheKey];
- }
- else
- {
- lock (typeCache)
- {
- if (string.IsNullOrEmpty(CustomDllName))
- {
- type = Type.GetType(className + "`" + types.Length, true).MakeGenericType(types);
- }
- else
- {
- var custom = GetCustomTypeByClass(className + "`" + types.Length);
- if (custom != null)
- {
- type = custom.MakeGenericType(types);
- }
- if (type == null)
- {
- type = Type.GetType(className + "`" + types.Length, true).MakeGenericType(types);
- }
- }
- Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
- if (!typeCache.ContainsKey(cacheKey))
- {
- typeCache.Add(cacheKey, type);
- }
- }
- }
- var result = (Restult)Activator.CreateInstance(type, true);
- return result;
- }
- private static Restult NoCacheGetCacheInstance<Restult>(string className, Type[] types)
- {
- Type type = null;
- if (string.IsNullOrEmpty(CustomDllName))
- {
- type = Type.GetType(className + "`" + types.Length, true).MakeGenericType(types);
- }
- else
- {
- var custom = GetCustomTypeByClass(className + "`" + types.Length);
- if (custom != null)
- {
- type = custom.MakeGenericType(types);
- }
- if (type == null)
- {
- type = Type.GetType(className + "`" + types.Length)?.MakeGenericType(types);
- if (type == null)
- {
- type = GetCustomDbType(className + "`" + types.Length, type).MakeGenericType(types);
- }
- }
- }
- var result = (Restult)Activator.CreateInstance(type, true);
- return result;
- }
- public static T CreateInstance<T>(string className)
- {
- try
- {
- if (NoCache)
- {
- return NoCacheGetCacheInstance<T>(className);
- }
- else
- {
- return GetCacheInstance<T>(className);
- }
- }
- catch
- {
- return NoCacheGetCacheInstance<T>(className);
- }
- }
- private static T GetCacheInstance<T>(string className)
- {
- Type type;
- if (typeCache.ContainsKey(className))
- {
- type = typeCache[className];
- }
- else
- {
- lock (typeCache)
- {
- if (string.IsNullOrEmpty(CustomDllName))
- {
- type = assembly.GetType(className);
- }
- else
- {
- type= GetCustomTypeByClass(className);
- if (type == null)
- {
- type = assembly.GetType(className);
- }
- }
- Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
- if (!typeCache.ContainsKey(className))
- {
- typeCache.Add(className, type);
- }
- }
- }
- var result = (T)Activator.CreateInstance(type, true);
- return result;
- }
- private static T NoCacheGetCacheInstance<T>(string className)
- {
- Type type = null;
- if (string.IsNullOrEmpty(CustomDllName))
- {
- type=assembly.GetType(className);
- }
- else
- {
- type = GetCustomTypeByClass(className);
- }
- if (type == null)
- {
- type = GetCustomDbType(className, type);
- }
- var result = (T)Activator.CreateInstance(type, true);
- return result;
- }
- private static Type GetCustomDbType(string className, Type type)
- {
- if (className.Replace(".", "").Length + 1 == className.Length)
- {
- var array = className.Split('.');
- foreach (var item in UtilMethods.EnumToDictionary<DbType>())
- {
- if (array.Last().StartsWith(item.Value.ToString()))
- {
- var newName = array.First() + "." + item.Value.ToString() + "." + array.Last();
- type = GetCustomTypeByClass(newName);
- break;
- }
- }
- }
- return type;
- }
- internal static Type GetCustomTypeByClass(string className)
- {
- Type type = null;
- foreach (var item in CustomDlls.ToArray())
- {
- if (type == null)
- {
- type = GetCustomTypeByClass(className, item);
- }
- if(type != null)
- {
- break;
- }
- }
- return type;
- }
- internal static Type GetCustomTypeByClass(string className,string customDllName)
- {
- var key = "Assembly_" + customDllName + assembly.GetHashCode();
- var newAssembly = new ReflectionInoCacheService().GetOrCreate<Assembly>(key, () => {
- try
- {
- if (CustomAssemblies?.Any(it => it.FullName.StartsWith(customDllName))==true)
- {
- return CustomAssemblies?.First(it => it.FullName.StartsWith(customDllName));
- }
- var path = Assembly.GetExecutingAssembly().Location;
- if (path.HasValue())
- {
- path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), customDllName + ".dll");
- }
- if (path.HasValue() && FileHelper.IsExistFile(path))
- {
- return Assembly.LoadFrom(path);
- }
- else
- {
- if (IsWebFrom)
- {
- string newpath = (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\" + customDllName + ".dll").Replace("file:\\", "");
- return Assembly.LoadFrom(newpath);
- }
- return Assembly.LoadFrom(customDllName + ".dll");
- }
- }
- catch
- {
- var message = "Not Found " + customDllName + ".dll";
- Check.Exception(true, message);
- return null;
- }
- });
- Type type = newAssembly.GetType(className);
- if (type == null)
- {
- type = assembly.GetType(className);
- }
- return type;
- }
- internal static Type GetCustomTypeByClass<T>(string className)
- {
- Type type = null;
- foreach (var item in CustomDlls.ToArray())
- {
- if (type == null)
- {
- type = GetCustomTypeByClass<T>(className, item);
- }
- if (type != null)
- {
- break;
- }
- }
- return type;
- }
- internal static Type GetCustomTypeByClass<T>(string className,string customDllName)
- {
- var key = "Assembly_" + customDllName + assembly.GetHashCode();
- var newAssembly = new ReflectionInoCacheService().GetOrCreate<Assembly>(key, () => {
- try
- {
- if (CustomAssemblies?.Any(it => it.FullName.StartsWith(customDllName)) == true)
- {
- return CustomAssemblies?.First(it => it.FullName.StartsWith(customDllName));
- }
- var path = Assembly.GetExecutingAssembly().Location;
- if (path.HasValue())
- {
- path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), customDllName + ".dll");
- }
- if (path.HasValue() && FileHelper.IsExistFile(path))
- {
- return Assembly.LoadFrom(path);
- }
- else
- {
- if (IsWebFrom)
- {
- string newpath = (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\" + customDllName + ".dll").Replace("file:\\", "");
- return Assembly.LoadFrom(newpath);
- }
- return Assembly.LoadFrom(customDllName + ".dll");
- }
- }
- catch
- {
- var message = "Not Found " + customDllName + ".dll";
- Check.Exception(true, message);
- return null;
- }
- });
- Type typeArgument = typeof(T);
- string fullTypeName = className + "[[" + typeArgument.FullName+","+ typeArgument.Assembly.FullName+ "]]";
- Type type = newAssembly.GetType(fullTypeName);
- return type;
- }
- #endregion
- }
- }
|