From 1cacd2c9823fc5da6e2634a78f009c5c37bd3847 Mon Sep 17 00:00:00 2001 From: Mina Asham Date: Sun, 8 Mar 2015 19:47:58 +0000 Subject: [PATCH] Adding algs4Console project, this will be used to start the required algorithm from console as C# doesn't allow multiple main methods. --- algs4.sln | 6 +++ algs4Console/Program.cs | 61 +++++++++++++++++++++++ algs4Console/Properties/AssemblyInfo.cs | 36 ++++++++++++++ algs4Console/algs4Console.csproj | 64 +++++++++++++++++++++++++ 4 files changed, 167 insertions(+) create mode 100644 algs4Console/Program.cs create mode 100644 algs4Console/Properties/AssemblyInfo.cs create mode 100644 algs4Console/algs4Console.csproj diff --git a/algs4.sln b/algs4.sln index e63f13c..6ca42a8 100644 --- a/algs4.sln +++ b/algs4.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 12.0.31101.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "algs4", "algs4\algs4.csproj", "{C784AA42-2AB6-414E-A4A4-7A24AAFBBDE2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "algs4Console", "algs4Console\algs4Console.csproj", "{297A04F6-7304-4427-9E60-6421BBE32E63}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {C784AA42-2AB6-414E-A4A4-7A24AAFBBDE2}.Debug|Any CPU.Build.0 = Debug|Any CPU {C784AA42-2AB6-414E-A4A4-7A24AAFBBDE2}.Release|Any CPU.ActiveCfg = Release|Any CPU {C784AA42-2AB6-414E-A4A4-7A24AAFBBDE2}.Release|Any CPU.Build.0 = Release|Any CPU + {297A04F6-7304-4427-9E60-6421BBE32E63}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {297A04F6-7304-4427-9E60-6421BBE32E63}.Debug|Any CPU.Build.0 = Debug|Any CPU + {297A04F6-7304-4427-9E60-6421BBE32E63}.Release|Any CPU.ActiveCfg = Release|Any CPU + {297A04F6-7304-4427-9E60-6421BBE32E63}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/algs4Console/Program.cs b/algs4Console/Program.cs new file mode 100644 index 0000000..b53c449 --- /dev/null +++ b/algs4Console/Program.cs @@ -0,0 +1,61 @@ +using System; +using System.Linq; +using System.Reflection; + +namespace algs4Console +{ + class Program + { + /// + /// Possible templates to extract RunMain methods from, usually class name is the name of the algorithm + /// + private static readonly string[] AssemblyTemplates = + { + "algs4.algs4.{0}`1[System.Object], algs4" , + "algs4.algs4.{0}, algs4", + "algs4.stdlib.{0}, algs4" + }; + + /// + /// The name of the method to extract and run from the desired class + /// + private const string ClassMainMethodName = "RunMain"; + + /// + /// Main method, runs the RunMethod for the class name + /// passed in the first argument, and passes the rest of the arguments + /// + /// Main arguments + static void Main(string[] args) + { + // Parse arguments + if (args.Length == 0) + { + Console.WriteLine("Usage: algs4Console.exe className [arg0[ args1[...]]]"); + return; + } + string className = args.First(); + string[] actualArgs = args.Skip(1).ToArray(); + + // Extract class + Type classType = AssemblyTemplates.Select(template => Type.GetType(string.Format(template, className))).FirstOrDefault(type => type != null); + if (classType == null) + { + Console.WriteLine("Class with name \"{0}\" was not found!", className); + return; + } + + // Extract RunMain method + MethodInfo mainMethod = classType.GetMethod(ClassMainMethodName, BindingFlags.Public | BindingFlags.Static); + if (mainMethod == null) + { + Console.WriteLine("Class \"{0}\" doesn't have \"RunMain\" method!", className); + Console.WriteLine("If you think this is a bug, please report it: https://github.com/mina-asham/algs4-CSharp"); + return; + } + + // Invoke method + mainMethod.Invoke(null, new object[] { actualArgs }); + } + } +} diff --git a/algs4Console/Properties/AssemblyInfo.cs b/algs4Console/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..86d2596 --- /dev/null +++ b/algs4Console/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("algs4Console")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("algs4Console")] +[assembly: AssemblyCopyright("Copyright © 2015")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("61307880-6684-4718-9101-8c8039cbe508")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/algs4Console/algs4Console.csproj b/algs4Console/algs4Console.csproj new file mode 100644 index 0000000..437a5ea --- /dev/null +++ b/algs4Console/algs4Console.csproj @@ -0,0 +1,64 @@ + + + + + Debug + AnyCPU + {297A04F6-7304-4427-9E60-6421BBE32E63} + Exe + Properties + algs4Console + algs4Console + v4.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + {C784AA42-2AB6-414E-A4A4-7A24AAFBBDE2} + algs4 + + + + + \ No newline at end of file