miniSql

创建
zgc123@gmail.com authored at 11/19/2023 1:40:15 AM
6136600
Tree
0 Parent(s)
Summary: 1 changed files with 68 additions and 0 deletions.
Added +68 -0
Added +68 -0
diff --git a/minisqlquery-master/src/MiniSqlQuery.Core/PlugInUtility.cs b/minisqlquery-master/src/MiniSqlQuery.Core/PlugInUtility.cs
new file mode 100644
index 0000000..413821c
--- /dev/null
+++ b/minisqlquery-master/src/MiniSqlQuery.Core/PlugInUtility.cs
@@ -0,0 +1,68 @@
+#region License
+
+// Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved.
+// This source code is made available under the terms of the GNU Lesser General Public License v3.0
+// https://github.com/paulkohler/minisqlquery/blob/master/LICENSE
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Reflection;
+
+namespace MiniSqlQuery.Core
+{
+    /// <summary>
+    /// 	Helper class for loading external plugins.
+    /// </summary>
+    public class PlugInUtility
+    {
+        /// <summary>
+        /// 	Search <paramref name = "baseDir" /> for files that match the pattern <paramref name = "searchPattern" />
+        /// 	and return an array of instances.
+        /// </summary>
+        /// <returns>An array of instances of the plugins found.</returns>
+        /// <typeparam name = "T">The type (interface or class) to find instances of.</typeparam>
+        /// <param name = "baseDir">The search base.</param>
+        /// <param name = "searchPattern">Search pattern, e.g. "*.dll".</param>
+        public static T[] GetInstances<T>(string baseDir, string searchPattern)
+        {
+            var tmpInstances = new List<T>();
+            Assembly pluginAssembly;
+
+            try
+            {
+                // perform the file search
+                string[] files = Directory.GetFiles(baseDir, searchPattern, SearchOption.TopDirectoryOnly);
+
+                // load each asembly and inspect for instances of T
+                foreach (string file in files)
+                {
+                    pluginAssembly = Assembly.LoadFrom(file);
+                    Type[] assemblyTypes = pluginAssembly.GetTypes();
+
+                    // check each assembly to se it it implements the interface T
+                    foreach (Type assemblyType in assemblyTypes)
+                    {
+                        Type instanceType = assemblyType.GetInterface(typeof(T).FullName);
+                        if (instanceType != null)
+                        {
+                            // this instance type matches T, create an instance of the class and add it to the list
+                            tmpInstances.Add((T)Activator.CreateInstance(assemblyType));
+                        }
+                    }
+                }
+            }
+            catch (TargetInvocationException exp)
+            {
+                if (exp.InnerException != null)
+                {
+                    throw exp.InnerException;
+                }
+            }
+
+            return tmpInstances.ToArray();
+        }
+    }
+}
\ No newline at end of file