Base Class to handle behavior, customize features, etc in Console Application?

此生再无相见时 提交于 2019-12-23 04:29:49

问题


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;
        }
    }

    /*....*/
}

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

  • Console.Title = Title;
  • static void WriteMessage(string message, TypeMessage typeMessage = TypeMessage.Log)
  • static void ExecuteAction(Action action)

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

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

How to achieve this?


回答1:


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);
  }
}

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);
  }
}



回答2:


Is Title a string field? It may be a better idea to make it into a property, that way every time that it is set it will update instantaneously.

Also I believe that the default Program class is C# is not a static class, so I don't really see why you're making a static constructor for your BaseProgram class. Main is able to access any static components of Program that you create (assuming proper access modifiers are given).

Good luck, I hope that I was able to help you out!



来源:https://stackoverflow.com/questions/21420606/base-class-to-handle-behavior-customize-features-etc-in-console-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!