コード例 #1
0
ファイル: Examples.cs プロジェクト: weloytty/MSMQ.Messaging
 private static void RunOne(IExample oneExample, string[] argumentsForExample)
 {
     try {
         oneExample.ExampleMessage += Example_OnExampleMessage;
         oneExample.RunExample(argumentsForExample);
         oneExample.ExampleMessage -= Example_OnExampleMessage;
     }
     catch (Exception e) {
         Console.WriteLine($"{oneExample.GetType().Name} threw exception {e.Message}");
     }
 }
コード例 #2
0
        public new bool Equals(object a, object b)
        {
            IExample ga = a as IExample;
            IExample gb = b as IExample;

            if (ga == null && gb == null)
            {
                return(true);
            }
            if (ga != null && gb != null && (ga.GetType() == gb.GetType()))
            {
                return(ga.Value == gb.Value);
            }
            return(false);
        }
コード例 #3
0
        public static void Execute(NestClient client, IExample example)
        {
            var exampleName = example.GetType().GetCustomAttribute <ExampleAttribute>()?.Name ?? example.GetType().Name;

            log.Info($"--------------------------------------------------------------------------------");
            log.Info($"---------------- EXAMPLE \"{exampleName}\" START");

            try
            {
                example.Execute(client);
            }
            catch (Exception ex)
            {
                log.Error("Failed to execute \"{exampleName}\".", ex);
            }
            finally
            {
                log.Info($"---------------- EXAMPLE \"{exampleName}\" END");
                log.Info($"--------------------------------------------------------------------------------");
                log.Info($"Presss ENTER to continue.");

                Console.ReadLine();
            }
        }
コード例 #4
0
        private List <string> GetSourceCode(IExample example, string methodName)
        {
            var className        = example.GetType().Name;
            var sourceCode       = new List <string>();
            var allLines         = File.ReadAllLines(Path.Combine(sourceFolder, $"{className}.cs"));
            var insideMainMethod = false;

            var syntaxTree =
                CSharpSyntaxTree.ParseText(File.ReadAllText(Path.Combine(sourceFolder, $"{className}.cs")));
            var root               = syntaxTree.GetRoot() as CompilationUnitSyntax;
            var namespaceSyntax    = root.Members.OfType <NamespaceDeclarationSyntax>().First();
            var programClassSyntax = namespaceSyntax.Members.OfType <ClassDeclarationSyntax>().First();
            var mainMethodSyntax   = programClassSyntax.Members.OfType <MethodDeclarationSyntax>().First();


            Console.WriteLine(mainMethodSyntax.ToString());

            //var regionNodesList = new List<RegionNodes>();
            //foreach (var regionDirective in root.DescendantTrivia()
            //    .Where(i => i.Kind() == SyntaxKind.RegionDirectiveTrivia))
            //    regionNodesList.Add(new RegionNodes {RegionDirective = regionDirective});
            //var count = regionNodesList.Count;
            //foreach (var endRegionDirective in root.DescendantTrivia()
            //    .Where(j => j.Kind() == SyntaxKind.EndRegionDirectiveTrivia))
            //    regionNodesList[--count].EndRegionDirective = endRegionDirective;
            //foreach (var node in root.DescendantNodes()
            //    .Where(i => i is MemberDeclarationSyntax || i is StatementSyntax))
            //foreach (var regionNodes in regionNodesList)
            //    regionNodes.AddNode(node);


            //foreach (var regionNode in regionNodesList)
            //{
            //    Console.WriteLine();

            //    var test = root.ToFullString();
            //    var test2 = root.GetText();
            //    var test3 = test2.GetSubText(regionNode.RegionSpan);

            //    Console.WriteLine(test3.ToString());
            //}

            Console.WriteLine();

            // get the region trivia
            var regions = root
                          .DescendantNodes(descendIntoTrivia: true)
                          .OfType <RegionDirectiveTriviaSyntax>()
                          .ToList();

            foreach (var region in regions)
            {
                var regionText = region.ToString().Replace("#region ", "");
                Console.WriteLine(regionText);

                Console.WriteLine(region.ToFullString());

                var test  = root.ToFullString();
                var test2 = root.GetText();
                var test3 = test2.GetSubText(region.Span);

                Console.WriteLine();
            }

            var sourceCodeParser = new SourceCodeParser(Path.Combine(sourceFolder, $"{className}.cs"));

            for (var i = 0; i < allLines.Length; i++)
            {
                var line = allLines[i];

                if (line.Contains(methodName) && line.Contains("public"))
                {
                    insideMainMethod = true;

                    if (!line.Contains("{"))
                    {
                        i++;
                    }
                }
                else if (line.StartsWith("        }") || line.StartsWith("            return"))
                {
                    insideMainMethod = false;
                }
                else if (insideMainMethod)
                {
                    if (line.StartsWith("            "))
                    {
                        line = line.Remove(0, "            ".Length);
                    }

                    sourceCode.Add(line);
                }
            }

            return(sourceCode);
        }