C# 创建静态的全局实例 Class
C# Create Global instance of a Static Class
我继承了一个代码库,我希望对 class 中的一些静态函数进行一些小改动,而不需要进行大量重构,以便能够与配置信息一起使用。
我的问题是有一个静态 class 具有与控制台交互的功能。然后可以使用 Console.Write() 符号简单地调用这些静态函数。我遇到的问题是我需要根据传递给主程序的命令行选项将一些配置更改放入静态 class。
虽然我通常会使用依赖注入来支持这一点,但代码库没有将此 class 的实例传递给使用它的对象。我需要在运行时在静态 class 中配置一个设置来控制 class 中的函数如何工作。
我不知道如何使用 C#(和其他语言)执行此操作而不进行更大的更改以支持依赖项注入。
静态 class 访问的简短示例
public class ConsoleUtilities
{
public static string ApplicationVersion
{
get
{
Assembly MyProgram = Assembly.GetEntryAssembly();
return MyProgram.GetName().Version.ToString();
}
}
/// <summary>
/// Show Text on the screen, and optionally write to LogPathFileName
/// </summary>
/// <param name="HelpText">Text to show</param>
/// <param name="LogPathFileName">Path and File Name of LogFile to write to. Use "" to not Log</param>
public static void ShowText(string[] HelpText, string LogPathFileName)
{
foreach (string HelpLine in HelpText)
{
ShowText(HelpLine, System.ConsoleColor.Gray, System.ConsoleColor.Black, LogPathFileName);
}
}
public static void ShowText(string HelpLine, System.ConsoleColor Foreground, System.ConsoleColor Background, string LogPathFileName)
{
ShowTextOnConsole(HelpLine, Foreground, Background, true, LogPathFileName);
}
}
使用静态的示例程序class
public class Program
{
public enum EXIT_CODES { OK = 0, CONFIG_ERROR, FILE_ERROR, COMPARE_ERROR, EXECUTION_ERROR, WARNING };
public const string LogPathFileName = "Tool.log";
static int Main(string[] args)
{
int ApplicationExitCode = (int)EXIT_CODES.OK;
int Failures = 0;
string[] AboutText = {
"Tool.exe - " + CSharpUtilities.ConsoleUtilities.ApplicationVersion,
"Copyright (c) 2014",
""
};
Settings AppSettings = new Settings(AboutText);
// Ensure Command Line is valid before proceeding
ApplicationExitCode = (int)EXIT_CODES.CONFIG_ERROR;
ApplicationCommandLine AppCommandLine = new ApplicationCommandLine();
try
{
if (AppCommandLine.ParseCommandLine(args))
{
// Load Application Settings
LoadApplicationSettings(ref AppSettings, AppCommandLine);
ConsoleUtilities.ShowText(AboutText, LogPathFileName);
List<string> ValidationErrors = ValidateApplicationSettings(AppSettings);
if (ValidationErrors.Count == 0)
{
...
}
else
{
ConsoleUtilities.ShowText(ValidationErrors.ToArray(), LogPathFileName);
}
}
}
catch (Exception e)
{
ApplicationExitCode = (int)EXIT_CODES.EXECUTION_ERROR; // Exception occured in processing. Fail the program execution.
string[] SystemError = { "System Error", e.Message.ToString() };
ConsoleUtilities.ShowText(SystemError, LogPathFileName);
}
}
}
你可以把静态字段放在static class中,解析命令行后在你的程序中初始化它们。例如:
public class ConsoleUtilities
{
public static bool ShowLog { get; set; } = true; // true, if we want log messages to be printed
public static void Log(string[] HelpText, string LogPathFileName)
{
if (ShowLog) {
foreach (string HelpLine in HelpText)
{
ShowText(HelpLine, System.ConsoleColor.Gray, System.ConsoleColor.Black, LogPathFileName);
}
}
}
}
然后在代码中某处解析命令行时,您将使用
ConsoleUtilities.ShowLog = true; // or false
您可以将配置公开为静态字段或 属性 :
public class ConsoleUtilities
{
public static string ApplicationVersion
{
get
{
Assembly MyProgram = Assembly.GetEntryAssembly();
return MyProgram.GetName().Version.ToString();
}
}
public static ConsoleConfiguration Configuration = new ConsoleConfiguration();
/// <summary>
/// Show Text on the screen, and optionally write to LogPathFileName
/// </summary>
/// <param name="HelpText">Text to show</param>
/// <param name="LogPathFileName">Path and File Name of LogFile to write to. Use "" to not Log</param>
public static void ShowText(string[] HelpText, string LogPathFileName)
{
foreach (string HelpLine in HelpText)
{
ShowText(HelpLine, Configuration.BackgroundColor, Configuration.ForegroundColor, LogPathFileName);
}
}
public static void ShowText(string HelpLine, System.ConsoleColor Foreground, System.ConsoleColor Background, string LogPathFileName)
{
ShowTextOnConsole(HelpLine, Foreground, Background, true, LogPathFileName);
}
}
public class ConsoleConfiguration
{
public ConsoleColor ForegroundColor { get; set; }
public ConsoleColor BackgroundColor { get; set; }
public ConsoleConfiguration()
{
ForegroundColor = ConsoleColor.Gray;
BackgroundColor = ConsoleColor.Black;
}
}
您可以像这样重新配置:
ConsoleUtilities.Configuration.ForegroundColor = ...
我继承了一个代码库,我希望对 class 中的一些静态函数进行一些小改动,而不需要进行大量重构,以便能够与配置信息一起使用。
我的问题是有一个静态 class 具有与控制台交互的功能。然后可以使用 Console.Write() 符号简单地调用这些静态函数。我遇到的问题是我需要根据传递给主程序的命令行选项将一些配置更改放入静态 class。
虽然我通常会使用依赖注入来支持这一点,但代码库没有将此 class 的实例传递给使用它的对象。我需要在运行时在静态 class 中配置一个设置来控制 class 中的函数如何工作。
我不知道如何使用 C#(和其他语言)执行此操作而不进行更大的更改以支持依赖项注入。
静态 class 访问的简短示例
public class ConsoleUtilities
{
public static string ApplicationVersion
{
get
{
Assembly MyProgram = Assembly.GetEntryAssembly();
return MyProgram.GetName().Version.ToString();
}
}
/// <summary>
/// Show Text on the screen, and optionally write to LogPathFileName
/// </summary>
/// <param name="HelpText">Text to show</param>
/// <param name="LogPathFileName">Path and File Name of LogFile to write to. Use "" to not Log</param>
public static void ShowText(string[] HelpText, string LogPathFileName)
{
foreach (string HelpLine in HelpText)
{
ShowText(HelpLine, System.ConsoleColor.Gray, System.ConsoleColor.Black, LogPathFileName);
}
}
public static void ShowText(string HelpLine, System.ConsoleColor Foreground, System.ConsoleColor Background, string LogPathFileName)
{
ShowTextOnConsole(HelpLine, Foreground, Background, true, LogPathFileName);
}
}
使用静态的示例程序class
public class Program
{
public enum EXIT_CODES { OK = 0, CONFIG_ERROR, FILE_ERROR, COMPARE_ERROR, EXECUTION_ERROR, WARNING };
public const string LogPathFileName = "Tool.log";
static int Main(string[] args)
{
int ApplicationExitCode = (int)EXIT_CODES.OK;
int Failures = 0;
string[] AboutText = {
"Tool.exe - " + CSharpUtilities.ConsoleUtilities.ApplicationVersion,
"Copyright (c) 2014",
""
};
Settings AppSettings = new Settings(AboutText);
// Ensure Command Line is valid before proceeding
ApplicationExitCode = (int)EXIT_CODES.CONFIG_ERROR;
ApplicationCommandLine AppCommandLine = new ApplicationCommandLine();
try
{
if (AppCommandLine.ParseCommandLine(args))
{
// Load Application Settings
LoadApplicationSettings(ref AppSettings, AppCommandLine);
ConsoleUtilities.ShowText(AboutText, LogPathFileName);
List<string> ValidationErrors = ValidateApplicationSettings(AppSettings);
if (ValidationErrors.Count == 0)
{
...
}
else
{
ConsoleUtilities.ShowText(ValidationErrors.ToArray(), LogPathFileName);
}
}
}
catch (Exception e)
{
ApplicationExitCode = (int)EXIT_CODES.EXECUTION_ERROR; // Exception occured in processing. Fail the program execution.
string[] SystemError = { "System Error", e.Message.ToString() };
ConsoleUtilities.ShowText(SystemError, LogPathFileName);
}
}
}
你可以把静态字段放在static class中,解析命令行后在你的程序中初始化它们。例如:
public class ConsoleUtilities
{
public static bool ShowLog { get; set; } = true; // true, if we want log messages to be printed
public static void Log(string[] HelpText, string LogPathFileName)
{
if (ShowLog) {
foreach (string HelpLine in HelpText)
{
ShowText(HelpLine, System.ConsoleColor.Gray, System.ConsoleColor.Black, LogPathFileName);
}
}
}
}
然后在代码中某处解析命令行时,您将使用
ConsoleUtilities.ShowLog = true; // or false
您可以将配置公开为静态字段或 属性 :
public class ConsoleUtilities
{
public static string ApplicationVersion
{
get
{
Assembly MyProgram = Assembly.GetEntryAssembly();
return MyProgram.GetName().Version.ToString();
}
}
public static ConsoleConfiguration Configuration = new ConsoleConfiguration();
/// <summary>
/// Show Text on the screen, and optionally write to LogPathFileName
/// </summary>
/// <param name="HelpText">Text to show</param>
/// <param name="LogPathFileName">Path and File Name of LogFile to write to. Use "" to not Log</param>
public static void ShowText(string[] HelpText, string LogPathFileName)
{
foreach (string HelpLine in HelpText)
{
ShowText(HelpLine, Configuration.BackgroundColor, Configuration.ForegroundColor, LogPathFileName);
}
}
public static void ShowText(string HelpLine, System.ConsoleColor Foreground, System.ConsoleColor Background, string LogPathFileName)
{
ShowTextOnConsole(HelpLine, Foreground, Background, true, LogPathFileName);
}
}
public class ConsoleConfiguration
{
public ConsoleColor ForegroundColor { get; set; }
public ConsoleColor BackgroundColor { get; set; }
public ConsoleConfiguration()
{
ForegroundColor = ConsoleColor.Gray;
BackgroundColor = ConsoleColor.Black;
}
}
您可以像这样重新配置:
ConsoleUtilities.Configuration.ForegroundColor = ...