如何使用控制台应用程序从 api 执行方法?

How to use a console application to execute methods from an api?

我想创建一个控制台应用程序,它可以从 API 中执行我的一些方法,以便人们更容易理解这些方法的工作原理。获取用户输入然后显示输出数据的最佳方式是什么。

这是我目前的情况:

public class SampleProgram
{
    private readonly IRestClient _client;
    private const string vTruckUrl = "api/trucktype";

    public SampleProgram(string serviceUrl)
    {
        _client = new RestClient(serviceUrl);
    }

    public List<TruckType> GetVisionTruckType(string orderItemNumber) 
    //TruckType is a simple model containting two strings.
    {
        var request = new RestRequest(vTruckUrl, Method.GET)
        {
            RequestFormat = DataFormat.Json
        };

        request.AddParameter("orderItemNumber", orderItemNumber);

        var response = _client.Execute<List<VisionTruckType>>(request);
        if (response.StatusCode == System.Net.HttpStatusCode.OK)
            return response.Data;

        throw new Exception("The truck type and job name could not be queried.");
    }
}

控制台:

public class ConsoleProgram
{

    private static SampleProgram _service = new SampleProgram("https://mytesturl.com/");
    static void Main(string[] args)
    {
        Console.WriteLine("");
        Console.WriteLine("1" = GetVisionTruckType Method");
        Console.WriteLine("");
        Console.WriteLine("Select the number that correlates to the Method that you want to execute: ");

        string methodNumber = string.Empty;
        while (methodNumber == string.Empty)
        {

            methodNumber = Console.ReadLine();
            if (!(methodNumber == "1"))
            {
                methodNumber = string.Empty;
                Console.WriteLine("Invalid input.");
                Console.WriteLine("Select the number that correlates to the Method that you want to execute: ");
            }
            switch (methodNumber)
            {

                case "1":
                    Console.WriteLine("Please enter a valid order item number: ");
                    string num = Console.ReadLine();
                    var type = _service.GetVisionTruckType(num);
                    Console.WriteLine(type.ToString());
                    break;

            }
            Console.Read();
        }
    }
 }

我的程序没有按照我的意愿输出列表。我该如何解决这个问题?

这是不正确的:

var type = _service.GetVisionTruckType(num);
Console.WriteLine(type.ToString());

默认情况下,ToString() 将 return 引用类型上的类型本身的名称。 (毕竟,对于任意 class,系统如何知道您 期望 的 class 的字符串表示形式?)

您需要从该 type 对象打印您想要打印的任何内容。 (顺便说一句,这确实应该称为 types,因为它是一个集合。错误的命名会增加您遇到的混乱。)例如:

var type = _service.GetVisionTruckType(num);
foreach (var obj in type)
    Console.WriteLine(obj.SomeProperty);

这将在该集合中的每个元素上打印 SomeProperty 的值。

您想打印什么由您决定。关键是 C# 不会仅仅通过调用 ToString() 就知道你想要什么,你必须明确地告诉它。


相反,您可以覆盖 ToString()您控制的任何class:

public override string ToString()
{
    // build and return what you want a string representation of this object to be
}

这将使您能够始终如一地对该类型的对象调用 ToString() 并获得您期望的字符串表示形式。

您需要迭代您的方法 Returns.

列表中的项目
switch (methodNumber)
{

    case "1":
        Console.WriteLine("Please enter a valid order item number: ");
        string num = Console.ReadLine();
        List<TruckType> type = _service.GetVisionTruckType(num);
        foreach (var item in type )
        {   // this will only work if you override the ToString() 
            // method in the class TruckType 
            Console.WriteLine(item.ToString());
        }
        break;
}

如果您只想打印 TruckType 的某些属性,您需要直接访问它们:

foreach (var item in type )
{
    Console.WriteLine(item.SomeProperty.ToString());
}

Here 有助于理解如何覆盖 ToString()。如果你想显示关于一个对象的所有信息,它实际上是很好的。