Pregunta

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?

¿Fue útil?

Solución

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

Otros consejos

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!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top