From: <Tor...@us...> - 2008-06-09 21:59:27
|
Revision: 234 http://acmcontester.svn.sourceforge.net/acmcontester/?rev=234&view=rev Author: Torax777 Date: 2008-06-09 14:59:33 -0700 (Mon, 09 Jun 2008) Log Message: ----------- Redesigned solution. Interface moved to plugins loader. Implemented using generics. Modified Paths: -------------- ACMServer/trunk/tasks/Load_from_dll/Plugin1/CreaterPlugin.cs ACMServer/trunk/tasks/Load_from_dll/Plugin1/Plugin1.csproj ACMServer/trunk/tasks/Load_from_dll/taskDll/PluginLoader.cs ACMServer/trunk/tasks/Load_from_dll/taskDll/Program.cs ACMServer/trunk/tasks/Load_from_dll/taskDll/taskDll.csproj ACMServer/trunk/tasks/Load_from_dll/taskDll.sln Added Paths: ----------- ACMServer/trunk/tasks/Load_from_dll/taskDll/ICreaterPlugin.cs Removed Paths: ------------- ACMServer/trunk/tasks/Load_from_dll/BasePlug/ Modified: ACMServer/trunk/tasks/Load_from_dll/Plugin1/CreaterPlugin.cs =================================================================== --- ACMServer/trunk/tasks/Load_from_dll/Plugin1/CreaterPlugin.cs 2008-06-09 13:31:09 UTC (rev 233) +++ ACMServer/trunk/tasks/Load_from_dll/Plugin1/CreaterPlugin.cs 2008-06-09 21:59:33 UTC (rev 234) @@ -1,7 +1,6 @@ using System; -using BasePlug; -namespace Dll.Plugins +namespace PluginsFramework { public class CreaterPlugin: ICreaterPlugin { Modified: ACMServer/trunk/tasks/Load_from_dll/Plugin1/Plugin1.csproj =================================================================== --- ACMServer/trunk/tasks/Load_from_dll/Plugin1/Plugin1.csproj 2008-06-09 13:31:09 UTC (rev 233) +++ ACMServer/trunk/tasks/Load_from_dll/Plugin1/Plugin1.csproj 2008-06-09 21:59:33 UTC (rev 234) @@ -37,9 +37,9 @@ <Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup> <ItemGroup> - <ProjectReference Include="..\BasePlug\BasePlug.csproj"> - <Project>{3DE17E44-206E-4FC6-A700-0EA0161A2DC4}</Project> - <Name>BasePlug</Name> + <ProjectReference Include="..\taskDll\taskDll.csproj"> + <Project>{0AC9B72D-57AE-4EF9-9745-837F8E4343E5}</Project> + <Name>taskDll</Name> </ProjectReference> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> Added: ACMServer/trunk/tasks/Load_from_dll/taskDll/ICreaterPlugin.cs =================================================================== --- ACMServer/trunk/tasks/Load_from_dll/taskDll/ICreaterPlugin.cs (rev 0) +++ ACMServer/trunk/tasks/Load_from_dll/taskDll/ICreaterPlugin.cs 2008-06-09 21:59:33 UTC (rev 234) @@ -0,0 +1,9 @@ +using System; + +namespace PluginsFramework +{ + public interface ICreaterPlugin + { + int calc(); + } +} Modified: ACMServer/trunk/tasks/Load_from_dll/taskDll/PluginLoader.cs =================================================================== --- ACMServer/trunk/tasks/Load_from_dll/taskDll/PluginLoader.cs 2008-06-09 13:31:09 UTC (rev 233) +++ ACMServer/trunk/tasks/Load_from_dll/taskDll/PluginLoader.cs 2008-06-09 21:59:33 UTC (rev 234) @@ -1,105 +1,159 @@ using System; using System.Collections.Generic; -using BasePlug; using System.Reflection; +using System.IO; -namespace Other +namespace PluginsFramework { - public class PluginLoader + public static class PluginsLoader<T> where T : class { - List<ICreaterPlugin> _plugins = new List<ICreaterPlugin>(); - /// <summary> - /// returns a list of all plugins loaded. + /// Try to load all the instances of type T in all the DLLs, + /// found at path /// </summary> - /// <value>The plugins.</value> - public List<ICreaterPlugin> Plugins + /// <param name="path">Path where to find appropriate DLLs</param> + /// <returns>List of all instaces found</returns> + public static List<T> Load(string path) { - get { return _plugins; } - } - /// <summary> - /// Loads all plugins. - /// </summary> - public void Load() - { - _plugins.Clear(); - try + List<T> plugins = new List<T>(); + if (!Directory.Exists(path)) + throw new ArgumentException("Specified path does not exists!"); + string[] allFiles = Directory.GetFiles(path, "*.dll", SearchOption.TopDirectoryOnly); + foreach (string file in allFiles) { - if (System.IO.Directory.Exists("Dll")) - { - string[] strFiles = System.IO.Directory.GetFiles("Dll", "*.dll"); - foreach (string strFile in strFiles) - LoadPlugin(strFile); - } + ScanAndLoad(file, plugins); } - catch (Exception ex) - { - throw ex; - } + return plugins; } + /// <summary> - /// Loads the plugin. + /// Load all instances of type T in specified file and + /// add them to provided list /// </summary> - /// <param name="strFile">The STR file.</param> - void LoadPlugin(string strFile) + /// <param name="filename">Which file to scan</param> + /// <param name="lst">Where to add found objects</param> + private static void ScanAndLoad(string filename, List<T> lst) { - Type[] foundInterfaces = null; + Assembly assembly = Assembly.LoadFrom(filename); + if (assembly != null) + { + Type[] types = assembly.GetExportedTypes(); - try - { - Assembly assem = Assembly.LoadFrom(strFile); - if (assem != null) + foreach (Type t in types) { - Type[] types = assem.GetExportedTypes(); - - foreach (Type t in types) + try { - try - { - if (t.IsClass) - { - if (t.IsAbstract) continue; - - Object newObj = null; - ICreaterPlugin plugin = null; - TypeFilter myFilter2 = new TypeFilter(MyInterfaceFilter); - try - { - foundInterfaces = t.FindInterfaces(myFilter2, "BasePlug.ICreaterPlugin"); - if (foundInterfaces.Length > 0) - { - newObj = (object)Activator.CreateInstance(t); - plugin = (ICreaterPlugin)newObj; - _plugins.Add(plugin); - } - } - catch (System.Reflection.TargetInvocationException ex) - { - throw ex; - } - catch (Exception ex) - { - throw ex; - } - } - } - catch (System.NullReferenceException ex) - { - throw ex; - } + if (t.IsClass && !t.IsAbstract && t.GetInterface(typeof(T).FullName) != null) + lst.Add((T)Activator.CreateInstance(t)); } + catch (System.Reflection.TargetInvocationException ex) + { + throw ex; + } + catch (Exception ex) + { + throw ex; + } } } - catch (Exception ex) - { - throw ex; - } } + } - bool MyInterfaceFilter(Type typeObj, Object criteriaObj) - { - return (typeObj.ToString().Equals(criteriaObj.ToString())); - } + //public class PluginLoader + //{ + // List<ICreaterPlugin> _plugins = new List<ICreaterPlugin>(); - } + // /// <summary> + // /// returns a list of all plugins loaded. + // /// </summary> + // /// <value>The plugins.</value> + // public List<ICreaterPlugin> Plugins + // { + // get { return _plugins; } + // } + // /// <summary> + // /// Loads all plugins. + // /// </summary> + // public void Load() + // { + // _plugins.Clear(); + // try + // { + // if (System.IO.Directory.Exists("Dll")) + // { + // string[] strFiles = System.IO.Directory.GetFiles("Dll", "*.dll"); + // foreach (string strFile in strFiles) + // LoadPlugin(strFile); + // } + // } + // catch (Exception ex) + // { + // throw ex; + // } + // } + // /// <summary> + // /// Loads the plugin. + // /// </summary> + // /// <param name="strFile">The STR file.</param> + // void LoadPlugin(string strFile) + // { + // Type[] foundInterfaces = null; + + // try + // { + // Assembly assem = Assembly.LoadFrom(strFile); + // if (assem != null) + // { + // Type[] types = assem.GetExportedTypes(); + + // foreach (Type t in types) + // { + // try + // { + // if (t.IsClass) + // { + // if (t.IsAbstract) continue; + + // Object newObj = null; + // ICreaterPlugin plugin = null; + // TypeFilter myFilter2 = new TypeFilter(MyInterfaceFilter); + // try + // { + // foundInterfaces = t.FindInterfaces(myFilter2, "BasePlug.ICreaterPlugin"); + // if (foundInterfaces.Length > 0) + // { + // newObj = (object)Activator.CreateInstance(t); + // plugin = (ICreaterPlugin)newObj; + // _plugins.Add(plugin); + // } + // } + // catch (System.Reflection.TargetInvocationException ex) + // { + // throw ex; + // } + // catch (Exception ex) + // { + // throw ex; + // } + // } + // } + // catch (System.NullReferenceException ex) + // { + // throw ex; + // } + // } + // } + // } + // catch (Exception ex) + // { + // throw ex; + // } + // } + + // bool MyInterfaceFilter(Type typeObj, Object criteriaObj) + // { + // return (typeObj.ToString().Equals(criteriaObj.ToString())); + // } + + //} } Modified: ACMServer/trunk/tasks/Load_from_dll/taskDll/Program.cs =================================================================== --- ACMServer/trunk/tasks/Load_from_dll/taskDll/Program.cs 2008-06-09 13:31:09 UTC (rev 233) +++ ACMServer/trunk/tasks/Load_from_dll/taskDll/Program.cs 2008-06-09 21:59:33 UTC (rev 234) @@ -1,8 +1,7 @@ using System; -using Other; -using BasePlug; +using System.Collections.Generic; -namespace taskDll +namespace PluginsFramework { class Program { @@ -14,10 +13,9 @@ /// <param name="args"></param> static void Main(string[] args) { - PluginLoader plugins = new PluginLoader(); - plugins.Load(); + List<ICreaterPlugin> plugins = PluginsLoader<ICreaterPlugin>.Load("Dll"); - foreach(ICreaterPlugin cp in plugins.Plugins) + foreach(ICreaterPlugin cp in plugins) { Console.Out.WriteLine(cp.calc()); } Modified: ACMServer/trunk/tasks/Load_from_dll/taskDll/taskDll.csproj =================================================================== --- ACMServer/trunk/tasks/Load_from_dll/taskDll/taskDll.csproj 2008-06-09 13:31:09 UTC (rev 233) +++ ACMServer/trunk/tasks/Load_from_dll/taskDll/taskDll.csproj 2008-06-09 21:59:33 UTC (rev 234) @@ -34,16 +34,11 @@ <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> + <Compile Include="ICreaterPlugin.cs" /> <Compile Include="PluginLoader.cs" /> <Compile Include="Program.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup> - <ItemGroup> - <ProjectReference Include="..\BasePlug\BasePlug.csproj"> - <Project>{3DE17E44-206E-4FC6-A700-0EA0161A2DC4}</Project> - <Name>BasePlug</Name> - </ProjectReference> - </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. Modified: ACMServer/trunk/tasks/Load_from_dll/taskDll.sln =================================================================== --- ACMServer/trunk/tasks/Load_from_dll/taskDll.sln 2008-06-09 13:31:09 UTC (rev 233) +++ ACMServer/trunk/tasks/Load_from_dll/taskDll.sln 2008-06-09 21:59:33 UTC (rev 234) @@ -3,12 +3,8 @@ # Visual Studio 2005 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "taskDll", "taskDll\taskDll.csproj", "{0AC9B72D-57AE-4EF9-9745-837F8E4343E5}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Dll", "Dll", "{CBD74D91-5EB9-4E4E-BF4B-D513ADD2B5D8}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Plugin1", "Plugin1\Plugin1.csproj", "{CDE3968A-F7DE-4938-A5C8-22B0949B8565}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasePlug", "BasePlug\BasePlug.csproj", "{3DE17E44-206E-4FC6-A700-0EA0161A2DC4}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -23,15 +19,8 @@ {CDE3968A-F7DE-4938-A5C8-22B0949B8565}.Debug|Any CPU.Build.0 = Debug|Any CPU {CDE3968A-F7DE-4938-A5C8-22B0949B8565}.Release|Any CPU.ActiveCfg = Release|Any CPU {CDE3968A-F7DE-4938-A5C8-22B0949B8565}.Release|Any CPU.Build.0 = Release|Any CPU - {3DE17E44-206E-4FC6-A700-0EA0161A2DC4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3DE17E44-206E-4FC6-A700-0EA0161A2DC4}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3DE17E44-206E-4FC6-A700-0EA0161A2DC4}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3DE17E44-206E-4FC6-A700-0EA0161A2DC4}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection - GlobalSection(NestedProjects) = preSolution - {CDE3968A-F7DE-4938-A5C8-22B0949B8565} = {CBD74D91-5EB9-4E4E-BF4B-D513ADD2B5D8} - EndGlobalSection EndGlobal This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |