本文介绍了在控制台应用程序中处理行为,自定义功能等的基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的控制台应用程序:

I have a Console Application like that:

public enum TypeMessage : int
{
    Log,
    Success,
    Warning,
    Alert
}

class Program
{
    static void Main(string[] args)
    {
        //do stuff
        WriteMessage("Sucess .... etc", TypeMessage.Success);
    }

    static void WriteMessage(string message, TypeMessage typeMessage = TypeMessage.Log)
    {
        switch (typeMessage)
        {
            case TypeMessage.Success:
                Console.ForegroundColor = ConsoleColor.Green;
                break;
            case TypeMessage.Warning:
                Console.ForegroundColor = ConsoleColor.Yellow;
                break;
            case TypeMessage.Alert:
                Console.ForegroundColor = ConsoleColor.Red;
                break;
            default:
                Console.ResetColor();
                break;
        }
        Console.WriteLine(message);
        Console.ResetColor();
    }

    static void ExecuteAction(Action action)
    {
        try
        {
            action.Invoke();
        }
        catch (Exception ex)
        {
            throw;
        }
    }

    /*....*/
}

我想创建一个BaseProgram类 class Program:BaseProgram 并放置初始化设置,帮助方法等:

I want to create a BaseProgram class class Program : BaseProgram and put there initialize settings, helpers method, etc:


  • Console.Title =标题;

  • 静态void WriteMessage(字符串消息,TypeMessage typeMessage = TypeMessage.Log)

  • 静态void ExecuteAction(动作)

我在BaseProgram类和设置中尝试了静态构造函数

I tried with a static constructor in BaseProgram class and setting the Console Title but didn't work.

static BaseProgram()
{
    Console.Title = "Some Title";
}

如何实现?

推荐答案

您当前使用静态构造函数的实现将无法正常工作,除非您在实例中调用/实例化 BaseProgram 或其派生类应用程序入口点( Program.Main())。

Your current implementation with static constructor won't work, unless you call/ instantiate the BaseProgram or its derivatives in your application entry point (Program.Main()).

class Program : BaseProgram
{
  static void Main(string[] args)
  {
     new BaseProgram(); // or, new Program();

     //do stuff
     WriteMessage("Sucess .... etc", TypeMessage.Success);
  }
}

话虽如此,而不是在上面使用静态构造函数 BaseProgram 也许您可以考虑像这样实现它:

Having said that, instead of using a static constructor on BaseProgram perhaps you could consider implementing it like this:

public abstract class BaseProgram
{      
  protected BaseProgram()
  {
    Console.Title = "Some Title";
    // other common initialization
  }

  public abstract void Run(string[] args);

  public void WriteMessage(string message, TypeMessage typeMessage = TypeMessage.Log)
  {
    ...
  }

  // other methods
}

public class Program : BaseProgram
{
  public static void Main(string[] args)
  {
    var program = new Program();
    program.Run(args);        
  } 

  public override void Run(string[] args)
  {
    //do stuff
    WriteMessage("Sucess .... etc", TypeMessage.Success);
  }
}

这篇关于在控制台应用程序中处理行为,自定义功能等的基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:46