InstanceFactory.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SqlSugar
  8. {
  9. public class InstanceFactory
  10. {
  11. static Assembly assembly = Assembly.GetExecutingAssembly();
  12. static Dictionary<string, Type> typeCache = new Dictionary<string, Type>();
  13. private static string _CustomDllName = "";
  14. private static List<string> CustomDlls = new List<string>();
  15. public static Assembly[] CustomAssemblies = new Assembly[]{};
  16. public static string CustomDllName {
  17. get { return _CustomDllName; }
  18. set
  19. {
  20. if (!CustomDlls.Contains(value))
  21. {
  22. CustomDlls.Add(value);
  23. }
  24. _CustomDllName = value;
  25. }
  26. }
  27. public static string CustomDbName = "";
  28. public static string CustomNamespace = "";
  29. public static bool NoCache = false;
  30. public static bool IsWebFrom = false;
  31. public static void RemoveCache()
  32. {
  33. typeCache = new Dictionary<string, Type>();
  34. }
  35. #region Queryable
  36. public static ISugarQueryable<T> GetQueryable<T>(ConnectionConfig currentConnectionConfig)
  37. {
  38. if (currentConnectionConfig.DbType == DbType.SqlServer)
  39. {
  40. return new SqlServerQueryable<T>();
  41. }
  42. else if (currentConnectionConfig.DbType == DbType.MySql)
  43. {
  44. return new MySqlQueryable<T>();
  45. }
  46. else if (currentConnectionConfig.DbType == DbType.PostgreSQL)
  47. {
  48. return new PostgreSQLQueryable<T>();
  49. }
  50. else
  51. {
  52. string className = "Queryable";
  53. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  54. ISugarQueryable<T> result = CreateInstance<T, ISugarQueryable<T>>(className);
  55. return result;
  56. }
  57. }
  58. public static ISugarQueryable<T, T2> GetQueryable<T, T2>(ConnectionConfig currentConnectionConfig)
  59. {
  60. if (currentConnectionConfig.DbType == DbType.SqlServer)
  61. {
  62. return new SqlServerQueryable<T, T2>();
  63. }
  64. else
  65. {
  66. string className = "Queryable";
  67. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  68. ISugarQueryable<T, T2> result = CreateInstance<T, T2, ISugarQueryable<T, T2>>(className);
  69. return result;
  70. }
  71. }
  72. public static ISugarQueryable<T, T2, T3> GetQueryable<T, T2, T3>(ConnectionConfig currentConnectionConfig)
  73. {
  74. if (currentConnectionConfig.DbType == DbType.SqlServer)
  75. {
  76. return new SqlServerQueryable<T, T2, T3>();
  77. }
  78. else
  79. {
  80. string className = "Queryable";
  81. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  82. ISugarQueryable<T, T2, T3> result = CreateInstance<T, T2, T3, ISugarQueryable<T, T2, T3>>(className);
  83. return result;
  84. }
  85. }
  86. public static ISugarQueryable<T, T2, T3, T4> GetQueryable<T, T2, T3, T4>(ConnectionConfig currentConnectionConfig)
  87. {
  88. string className = "Queryable";
  89. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  90. ISugarQueryable<T, T2, T3, T4> result = CreateInstance<T, T2, T3, T4, ISugarQueryable<T, T2, T3, T4>>(className);
  91. return result;
  92. }
  93. public static ISugarQueryable<T, T2, T3, T4, T5> GetQueryable<T, T2, T3, T4, T5>(ConnectionConfig currentConnectionConfig)
  94. {
  95. string className = "Queryable";
  96. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  97. ISugarQueryable<T, T2, T3, T4, T5> result = CreateInstance<T, T2, T3, T4, T5, ISugarQueryable<T, T2, T3, T4, T5>>(className);
  98. return result;
  99. }
  100. public static ISugarQueryable<T, T2, T3, T4, T5, T6> GetQueryable<T, T2, T3, T4, T5, T6>(ConnectionConfig currentConnectionConfig)
  101. {
  102. string className = "Queryable";
  103. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  104. ISugarQueryable<T, T2, T3, T4, T5, T6> result = CreateInstance<T, T2, T3, T4, T5, T6, ISugarQueryable<T, T2, T3, T4, T5, T6>>(className);
  105. return result;
  106. }
  107. public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7> GetQueryable<T, T2, T3, T4, T5, T6, T7>(ConnectionConfig currentConnectionConfig)
  108. {
  109. string className = "Queryable";
  110. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  111. 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);
  112. return result;
  113. }
  114. public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8>(ConnectionConfig currentConnectionConfig)
  115. {
  116. string className = "Queryable";
  117. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  118. 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);
  119. return result;
  120. }
  121. #region 9-12
  122. public static ISugarQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9> GetQueryable<T, T2, T3, T4, T5, T6, T7, T8, T9>(ConnectionConfig currentConnectionConfig)
  123. {
  124. string className = "Queryable";
  125. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  126. 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);
  127. return result;
  128. }
  129. 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)
  130. {
  131. string className = "Queryable";
  132. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  133. 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);
  134. return result;
  135. }
  136. 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)
  137. {
  138. string className = "Queryable";
  139. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  140. 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);
  141. return result;
  142. }
  143. 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)
  144. {
  145. string className = "Queryable";
  146. className = GetClassName(currentConnectionConfig.DbType.ToString(), className);
  147. 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);
  148. return result;
  149. }
  150. #endregion
  151. #endregion
  152. public static QueryBuilder GetQueryBuilderWithContext(ISqlSugarClient db)
  153. {
  154. if (db is SqlSugarClient)
  155. {
  156. db = (db as SqlSugarClient).Context;
  157. }
  158. else if (db is SqlSugarScope)
  159. {
  160. db = (db as SqlSugarScope).ScopedContext.Context;
  161. }
  162. if (!(db is SqlSugarProvider))
  163. {
  164. db = new SqlSugarClient(db.CurrentConnectionConfig).Context;
  165. }
  166. var QueryBuilder = InstanceFactory.GetQueryBuilder(db.CurrentConnectionConfig);
  167. QueryBuilder.Context = (SqlSugarProvider)db;
  168. QueryBuilder.Builder = InstanceFactory.GetSqlbuilder(db.CurrentConnectionConfig);
  169. QueryBuilder.Builder.Context = (SqlSugarProvider)db;
  170. return QueryBuilder;
  171. }
  172. public static QueryBuilder GetQueryBuilder(ConnectionConfig currentConnectionConfig)
  173. {
  174. if (currentConnectionConfig.DbType == DbType.SqlServer)
  175. {
  176. return new SqlServerQueryBuilder();
  177. }
  178. else if (currentConnectionConfig.DbType == DbType.MySql)
  179. {
  180. return new MySqlQueryBuilder();
  181. }
  182. else
  183. {
  184. QueryBuilder result = CreateInstance<QueryBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), "QueryBuilder"));
  185. return result;
  186. }
  187. }
  188. public static InsertBuilder GetInsertBuilder(ConnectionConfig currentConnectionConfig)
  189. {
  190. InsertBuilder result = CreateInstance<InsertBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), "InsertBuilder"));
  191. return result;
  192. }
  193. public static UpdateBuilder GetUpdateBuilder(ConnectionConfig currentConnectionConfig)
  194. {
  195. UpdateBuilder result = CreateInstance<UpdateBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), "UpdateBuilder"));
  196. return result;
  197. }
  198. public static DeleteBuilder GetDeleteBuilder(ConnectionConfig currentConnectionConfig)
  199. {
  200. DeleteBuilder result = CreateInstance<DeleteBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), "DeleteBuilder"));
  201. return result;
  202. }
  203. public static ILambdaExpressions GetLambdaExpressions(ConnectionConfig currentConnectionConfig)
  204. {
  205. if (currentConnectionConfig.DbType == DbType.SqlServer)
  206. {
  207. return new SqlServerExpressionContext();
  208. }
  209. else if (currentConnectionConfig.DbType == DbType.MySql)
  210. {
  211. return new MySqlExpressionContext();
  212. }
  213. else
  214. {
  215. ILambdaExpressions result = CreateInstance<ILambdaExpressions>(GetClassName(currentConnectionConfig.DbType.ToString(), "ExpressionContext"));
  216. return result;
  217. }
  218. }
  219. public static ISqlBuilder GetSqlbuilder(ConnectionConfig currentConnectionConfig)
  220. {
  221. if (currentConnectionConfig.DbType == DbType.SqlServer)
  222. {
  223. return new SqlServerBuilder();
  224. }
  225. else if (currentConnectionConfig.DbType == DbType.MySql)
  226. {
  227. return new MySqlBuilder();
  228. }
  229. else
  230. {
  231. ISqlBuilder result = CreateInstance<ISqlBuilder>(GetClassName(currentConnectionConfig.DbType.ToString(), "Builder"));
  232. return result;
  233. }
  234. }
  235. public static UpdateableProvider<T> GetUpdateableProvider<T>(ConnectionConfig currentConnectionConfig) where T : class, new()
  236. {
  237. if (currentConnectionConfig.DbType == DbType.Oracle)
  238. {
  239. return new OracleUpdateable<T>();
  240. }
  241. else
  242. {
  243. return new UpdateableProvider<T>();
  244. }
  245. }
  246. public static DeleteableProvider<T> GetDeleteableProvider<T>(ConnectionConfig currentConnectionConfig) where T : class, new()
  247. {
  248. if (currentConnectionConfig.DbType == DbType.Oracle)
  249. {
  250. return new OracleDeleteable<T>();
  251. }
  252. else
  253. {
  254. return new DeleteableProvider<T>();
  255. }
  256. }
  257. public static InsertableProvider<T> GetInsertableProvider<T>(ConnectionConfig currentConnectionConfig) where T : class, new()
  258. {
  259. if (currentConnectionConfig.DbType == DbType.Oracle)
  260. {
  261. return new OracleInsertable<T>();
  262. }
  263. else if (currentConnectionConfig.DbType == DbType.PostgreSQL)
  264. {
  265. return new PostgreSQLInserttable<T>();
  266. }
  267. else if (currentConnectionConfig.DbType == DbType.Kdbndp)
  268. {
  269. return new KdbndpInserttable<T>();
  270. }
  271. else if (currentConnectionConfig.DbType == DbType.Oscar)
  272. {
  273. return new KdbndpInserttable<T>();
  274. }
  275. else if (IsCustomDb(currentConnectionConfig))
  276. {
  277. var name =
  278. "SqlSugar." + currentConnectionConfig.DbType +
  279. "." + currentConnectionConfig.DbType
  280. + "Insertable`1";
  281. var type = GetCustomTypeByClass<T>(name);
  282. if (type == null)
  283. {
  284. return new InsertableProvider<T>();
  285. }
  286. else
  287. {
  288. return (InsertableProvider<T>)Activator.CreateInstance(type, true);
  289. }
  290. }
  291. else
  292. {
  293. return new InsertableProvider<T>();
  294. }
  295. }
  296. private static bool IsCustomDb(ConnectionConfig currentConnectionConfig)
  297. {
  298. return
  299. currentConnectionConfig.DbType != DbType.SqlServer &&
  300. currentConnectionConfig.DbType != DbType.Dm &&
  301. currentConnectionConfig.DbType != DbType.Oscar &&
  302. currentConnectionConfig.DbType != DbType.Access &&
  303. currentConnectionConfig.DbType != DbType.QuestDB &&
  304. currentConnectionConfig.DbType != DbType.MySql &&
  305. currentConnectionConfig.DbType != DbType.Oracle &&
  306. currentConnectionConfig.DbType != DbType.PostgreSQL &&
  307. currentConnectionConfig.DbType != DbType.ClickHouse &&
  308. currentConnectionConfig.DbType != DbType.GBase &&
  309. currentConnectionConfig.DbType != DbType.Sqlite &&
  310. GetCustomTypeByClass("SqlSugar." + currentConnectionConfig.DbType + "." + currentConnectionConfig.DbType + "Provider") != null;
  311. }
  312. public static IDbBind GetDbBind(ConnectionConfig currentConnectionConfig)
  313. {
  314. if (currentConnectionConfig.DbType == DbType.SqlServer)
  315. {
  316. return new SqlServerDbBind();
  317. }
  318. else if (currentConnectionConfig.DbType == DbType.MySql)
  319. {
  320. return new MySqlDbBind();
  321. }
  322. else
  323. {
  324. IDbBind result = CreateInstance<IDbBind>(GetClassName(currentConnectionConfig.DbType.ToString(), "DbBind"));
  325. return result;
  326. }
  327. }
  328. public static IDbMaintenance GetDbMaintenance(ConnectionConfig currentConnectionConfig)
  329. {
  330. IDbMaintenance result = CreateInstance<IDbMaintenance>(GetClassName(currentConnectionConfig.DbType.ToString(), "DbMaintenance"));
  331. return result;
  332. }
  333. public static IDbFirst GetDbFirst(ConnectionConfig currentConnectionConfig)
  334. {
  335. IDbFirst result = CreateInstance<IDbFirst>(GetClassName(currentConnectionConfig.DbType.ToString(), "DbFirst"));
  336. return result;
  337. }
  338. public static ICodeFirst GetCodeFirst(ConnectionConfig currentConnectionConfig)
  339. {
  340. ICodeFirst result = CreateInstance<ICodeFirst>(GetClassName(currentConnectionConfig.DbType.ToString(), "CodeFirst"));
  341. return result;
  342. }
  343. public static IAdo GetAdo(ConnectionConfig currentConnectionConfig)
  344. {
  345. if (currentConnectionConfig.DbType == DbType.SqlServer)
  346. {
  347. return new SqlServerProvider();
  348. }
  349. else
  350. {
  351. IAdo result = CreateInstance<IAdo>(GetClassName(currentConnectionConfig.DbType.ToString(), "Provider"));
  352. return result;
  353. }
  354. }
  355. private static string GetClassName(string type, string name)
  356. {
  357. if (type == "MySqlConnector")
  358. {
  359. return "SqlSugar.MySqlConnector.MySql" + name;
  360. }
  361. else if (type == "Access")
  362. {
  363. return "SqlSugar.Access.Access" + name;
  364. }
  365. else if (type == "ClickHouse")
  366. {
  367. return "SqlSugar.ClickHouse.ClickHouse" + name;
  368. }
  369. else if (type == "GBase")
  370. {
  371. return "SqlSugar.GBase.GBase" + name;
  372. }
  373. else if (type == "Odbc")
  374. {
  375. return "SqlSugar.Odbc.Odbc" + name;
  376. }
  377. else if (type == "Custom")
  378. {
  379. return CustomNamespace + "."+CustomDbName + name;
  380. }
  381. else
  382. {
  383. //if (!string.IsNullOrEmpty(CustomDllName))
  384. //{
  385. // type = CustomDllName;
  386. //}
  387. return UtilConstants.AssemblyName + "." + type + name;
  388. }
  389. }
  390. #region CreateInstance
  391. private static Restult CreateInstance<T, Restult>(string className)
  392. {
  393. return CreateInstance<Restult>(className, typeof(T));
  394. }
  395. private static Restult CreateInstance<T, T2, Restult>(string className)
  396. {
  397. return CreateInstance<Restult>(className, typeof(T), typeof(T2));
  398. }
  399. private static Restult CreateInstance<T, T2, T3, Restult>(string className)
  400. {
  401. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3));
  402. }
  403. private static Restult CreateInstance<T, T2, T3, T4, Restult>(string className)
  404. {
  405. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4));
  406. }
  407. private static Restult CreateInstance<T, T2, T3, T4, T5, Restult>(string className)
  408. {
  409. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5));
  410. }
  411. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, Restult>(string className)
  412. {
  413. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6));
  414. }
  415. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, Restult>(string className)
  416. {
  417. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7));
  418. }
  419. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, Restult>(string className)
  420. {
  421. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8));
  422. }
  423. #region 9-12
  424. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, Restult>(string className)
  425. {
  426. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9));
  427. }
  428. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, Restult>(string className)
  429. {
  430. return CreateInstance<Restult>(className, typeof(T), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8), typeof(T9), typeof(T10));
  431. }
  432. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Restult>(string className)
  433. {
  434. 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));
  435. }
  436. private static Restult CreateInstance<T, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Restult>(string className)
  437. {
  438. 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));
  439. }
  440. #endregion
  441. private static Restult CreateInstance<Restult>(string className, params Type[] types)
  442. {
  443. try
  444. {
  445. if (NoCache)
  446. {
  447. return NoCacheGetCacheInstance<Restult>(className, types);
  448. }
  449. else
  450. {
  451. return GetCacheInstance<Restult>(className, types);
  452. }
  453. }
  454. catch
  455. {
  456. NoCache = true;
  457. return NoCacheGetCacheInstance<Restult>(className, types);
  458. }
  459. }
  460. private static Restult GetCacheInstance<Restult>(string className, Type[] types)
  461. {
  462. var cacheKey = className + string.Join(",", types.Select(it => it.FullName));
  463. Type type=null;
  464. if (typeCache.ContainsKey(cacheKey))
  465. {
  466. type = typeCache[cacheKey];
  467. }
  468. else
  469. {
  470. lock (typeCache)
  471. {
  472. if (string.IsNullOrEmpty(CustomDllName))
  473. {
  474. type = Type.GetType(className + "`" + types.Length, true).MakeGenericType(types);
  475. }
  476. else
  477. {
  478. var custom = GetCustomTypeByClass(className + "`" + types.Length);
  479. if (custom != null)
  480. {
  481. type = custom.MakeGenericType(types);
  482. }
  483. if (type == null)
  484. {
  485. type = Type.GetType(className + "`" + types.Length, true).MakeGenericType(types);
  486. }
  487. }
  488. Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
  489. if (!typeCache.ContainsKey(cacheKey))
  490. {
  491. typeCache.Add(cacheKey, type);
  492. }
  493. }
  494. }
  495. var result = (Restult)Activator.CreateInstance(type, true);
  496. return result;
  497. }
  498. private static Restult NoCacheGetCacheInstance<Restult>(string className, Type[] types)
  499. {
  500. Type type = null;
  501. if (string.IsNullOrEmpty(CustomDllName))
  502. {
  503. type = Type.GetType(className + "`" + types.Length, true).MakeGenericType(types);
  504. }
  505. else
  506. {
  507. var custom = GetCustomTypeByClass(className + "`" + types.Length);
  508. if (custom != null)
  509. {
  510. type = custom.MakeGenericType(types);
  511. }
  512. if (type == null)
  513. {
  514. type = Type.GetType(className + "`" + types.Length)?.MakeGenericType(types);
  515. if (type == null)
  516. {
  517. type = GetCustomDbType(className + "`" + types.Length, type).MakeGenericType(types);
  518. }
  519. }
  520. }
  521. var result = (Restult)Activator.CreateInstance(type, true);
  522. return result;
  523. }
  524. public static T CreateInstance<T>(string className)
  525. {
  526. try
  527. {
  528. if (NoCache)
  529. {
  530. return NoCacheGetCacheInstance<T>(className);
  531. }
  532. else
  533. {
  534. return GetCacheInstance<T>(className);
  535. }
  536. }
  537. catch
  538. {
  539. return NoCacheGetCacheInstance<T>(className);
  540. }
  541. }
  542. private static T GetCacheInstance<T>(string className)
  543. {
  544. Type type;
  545. if (typeCache.ContainsKey(className))
  546. {
  547. type = typeCache[className];
  548. }
  549. else
  550. {
  551. lock (typeCache)
  552. {
  553. if (string.IsNullOrEmpty(CustomDllName))
  554. {
  555. type = assembly.GetType(className);
  556. }
  557. else
  558. {
  559. type= GetCustomTypeByClass(className);
  560. if (type == null)
  561. {
  562. type = assembly.GetType(className);
  563. }
  564. }
  565. Check.ArgumentNullException(type, string.Format(ErrorMessage.ObjNotExist, className));
  566. if (!typeCache.ContainsKey(className))
  567. {
  568. typeCache.Add(className, type);
  569. }
  570. }
  571. }
  572. var result = (T)Activator.CreateInstance(type, true);
  573. return result;
  574. }
  575. private static T NoCacheGetCacheInstance<T>(string className)
  576. {
  577. Type type = null;
  578. if (string.IsNullOrEmpty(CustomDllName))
  579. {
  580. type=assembly.GetType(className);
  581. }
  582. else
  583. {
  584. type = GetCustomTypeByClass(className);
  585. }
  586. if (type == null)
  587. {
  588. type = GetCustomDbType(className, type);
  589. }
  590. var result = (T)Activator.CreateInstance(type, true);
  591. return result;
  592. }
  593. private static Type GetCustomDbType(string className, Type type)
  594. {
  595. if (className.Replace(".", "").Length + 1 == className.Length)
  596. {
  597. var array = className.Split('.');
  598. foreach (var item in UtilMethods.EnumToDictionary<DbType>())
  599. {
  600. if (array.Last().StartsWith(item.Value.ToString()))
  601. {
  602. var newName = array.First() + "." + item.Value.ToString() + "." + array.Last();
  603. type = GetCustomTypeByClass(newName);
  604. break;
  605. }
  606. }
  607. }
  608. return type;
  609. }
  610. internal static Type GetCustomTypeByClass(string className)
  611. {
  612. Type type = null;
  613. foreach (var item in CustomDlls.ToArray())
  614. {
  615. if (type == null)
  616. {
  617. type = GetCustomTypeByClass(className, item);
  618. }
  619. if(type != null)
  620. {
  621. break;
  622. }
  623. }
  624. return type;
  625. }
  626. internal static Type GetCustomTypeByClass(string className,string customDllName)
  627. {
  628. var key = "Assembly_" + customDllName + assembly.GetHashCode();
  629. var newAssembly = new ReflectionInoCacheService().GetOrCreate<Assembly>(key, () => {
  630. try
  631. {
  632. if (CustomAssemblies?.Any(it => it.FullName.StartsWith(customDllName))==true)
  633. {
  634. return CustomAssemblies?.First(it => it.FullName.StartsWith(customDllName));
  635. }
  636. var path = Assembly.GetExecutingAssembly().Location;
  637. if (path.HasValue())
  638. {
  639. path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), customDllName + ".dll");
  640. }
  641. if (path.HasValue() && FileHelper.IsExistFile(path))
  642. {
  643. return Assembly.LoadFrom(path);
  644. }
  645. else
  646. {
  647. if (IsWebFrom)
  648. {
  649. string newpath = (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\" + customDllName + ".dll").Replace("file:\\", "");
  650. return Assembly.LoadFrom(newpath);
  651. }
  652. return Assembly.LoadFrom(customDllName + ".dll");
  653. }
  654. }
  655. catch
  656. {
  657. var message = "Not Found " + customDllName + ".dll";
  658. Check.Exception(true, message);
  659. return null;
  660. }
  661. });
  662. Type type = newAssembly.GetType(className);
  663. if (type == null)
  664. {
  665. type = assembly.GetType(className);
  666. }
  667. return type;
  668. }
  669. internal static Type GetCustomTypeByClass<T>(string className)
  670. {
  671. Type type = null;
  672. foreach (var item in CustomDlls.ToArray())
  673. {
  674. if (type == null)
  675. {
  676. type = GetCustomTypeByClass<T>(className, item);
  677. }
  678. if (type != null)
  679. {
  680. break;
  681. }
  682. }
  683. return type;
  684. }
  685. internal static Type GetCustomTypeByClass<T>(string className,string customDllName)
  686. {
  687. var key = "Assembly_" + customDllName + assembly.GetHashCode();
  688. var newAssembly = new ReflectionInoCacheService().GetOrCreate<Assembly>(key, () => {
  689. try
  690. {
  691. if (CustomAssemblies?.Any(it => it.FullName.StartsWith(customDllName)) == true)
  692. {
  693. return CustomAssemblies?.First(it => it.FullName.StartsWith(customDllName));
  694. }
  695. var path = Assembly.GetExecutingAssembly().Location;
  696. if (path.HasValue())
  697. {
  698. path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), customDllName + ".dll");
  699. }
  700. if (path.HasValue() && FileHelper.IsExistFile(path))
  701. {
  702. return Assembly.LoadFrom(path);
  703. }
  704. else
  705. {
  706. if (IsWebFrom)
  707. {
  708. string newpath = (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\" + customDllName + ".dll").Replace("file:\\", "");
  709. return Assembly.LoadFrom(newpath);
  710. }
  711. return Assembly.LoadFrom(customDllName + ".dll");
  712. }
  713. }
  714. catch
  715. {
  716. var message = "Not Found " + customDllName + ".dll";
  717. Check.Exception(true, message);
  718. return null;
  719. }
  720. });
  721. Type typeArgument = typeof(T);
  722. string fullTypeName = className + "[[" + typeArgument.FullName+","+ typeArgument.Assembly.FullName+ "]]";
  723. Type type = newAssembly.GetType(fullTypeName);
  724. return type;
  725. }
  726. #endregion
  727. }
  728. }