.NET Core 2.1 以下的控制台应用程序生成 EXE,且使用命令行参数动态运行控制器应用程序的示例


 

文章:

https://stackoverflow.com/questions/44038847/vs2017-compile-netcoreapp-as-exe

 

引用

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.CommandLineUtils" Version="1.1.1" />
    <PackageReference Include="System.Collections.NonGeneric" Version="4.3.0" />
  </ItemGroup>

Program.cs

using Microsoft.Extensions.CommandLineUtils;

namespace EnumerableSample
{
    class Program
    {
        static void Main(string[] args)
        {
            var app = new CommandLineApplication(throwOnUnexpectedArg: false);
            app.FullName = "LINQ Sample App";
            LinqSamples.Register(app);
            FilteringSamples.Register(app);
            GroupingSamples.Register(app);
            CompoundFromSamples.Register(app);
            JoinSamples.Register(app);
            SortingSamples.Register(app);

            app.Command("help", cmd =>
            {
                cmd.Description = "Get help for the application";
                CommandArgument commandArgument = cmd.Argument("<COMMAND>", "The command to get help for");
                cmd.OnExecute(() =>
                {
                    app.ShowHelp(commandArgument.Value);
                    return 0;
                });
            });

            app.OnExecute(() =>
            {
                app.ShowHelp();
                return 0;
            });

            app.Execute(args);
        }
    }
}

 

LinqSamples.cs

using Microsoft.Extensions.CommandLineUtils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace EnumerableSample
{
    internal class LinqSamples
    {
        internal static void Register(CommandLineApplication app)
        {
            MethodInfo[] methods = Assembly.GetExecutingAssembly()
                .GetTypes()
                .Where(t => t.Name == nameof(LinqSamples))
                .Single()
                .GetMethods()
                .Where(m => m.IsPublic && m.IsStatic)
                .ToArray();

            foreach (var method in methods)
            {
                app.Command(method.Name.ToLower(), cmd =>
                {
                    cmd.Description = method.Name;
                    cmd.OnExecute(() => { method.Invoke(null, null); return 0; });
                });
            }
        }

        public static void GenerateRange()
        {
            var values = Enumerable.Range(1, 20);
            foreach (var item in values)
            {
                Console.Write($"{item} ", item);
            }
            Console.WriteLine();
        }
    }
}

 

FilteringSamples.cs

using DataLib;
using Microsoft.Extensions.CommandLineUtils;
using System;
using System.Linq;

namespace EnumerableSample
{
    public class FilteringSamples
    {
        internal static void Register(CommandLineApplication app)
        {
            app.Command("filter", cmd =>
            {
                var invokeMethodOption = new CommandOption("-m", CommandOptionType.NoValue);
                var indexOption = new CommandOption("-i", CommandOptionType.NoValue);
                var typeOption = new CommandOption("-t", CommandOptionType.NoValue);
                cmd.Options.AddRange(new[] { invokeMethodOption, indexOption, typeOption });
                cmd.Description = "filter -[m|i|t]";
                cmd.OnExecute(() =>
                {
                    if (invokeMethodOption.HasValue())
                    {
                        FilteringWithMethods();
                    }
                    else if (indexOption.HasValue())
                    {
                        FilteringWithIndex();
                    }
                    else if (typeOption.HasValue())
                    {
                        TypeFiltering();
                    }
                    else
                    {
                        Filtering();
                    }
                    return 0;
                });
            });
        }

        public static void Filtering()
        {
            var racers = from r in Formula1.GetChampions()
                         where r.Wins > 15 && (r.Country == "Brazil" || r.Country == "Austria")
                         select r;

            foreach (var r in racers)
            {
                Console.WriteLine($"{r:A}");
            }
        }

        public static void FilteringWithIndex()
        {
            var racers = Formula1.GetChampions()
                .Where((r, index) => r.LastName.StartsWith("A") && index % 2 != 0);
            foreach (var r in racers)
            {
                Console.WriteLine($"{r:A}");
            }
        }

        public static void FilteringWithMethods()
        {
            var racers = Formula1.GetChampions()
                .Where(r => r.Wins > 15 && (r.Country == "Brazil" || r.Country == "Austria"));

            foreach (var r in racers)
            {
                Console.WriteLine($"{r:A}");
            }
        }

        public static void TypeFiltering()
        {
            object[] data = { "one", 2, 3, "four", "five", 6 };
            var query = data.OfType<string>();
            foreach (var s in query)
            {
                Console.WriteLine(s);
            }
        }
    }
}

 

 

 

运行截图:

 

 

 

谢谢浏览!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM