方法GetServer()返回null,原因未知

本文关键字:未知 null 返回 GetServer 方法 | 更新日期: 2023-09-27 18:13:51

我有一个项目,其中一个主要方法是返回null,当它很清楚地看到我已经分配了一个值给它。

Program.cs:

using System;
namespace Sahara
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Title = "Loading Sahara...";
            Console.CursorVisible = false;
            Sahara.Initialize();
            while (true)
            {
                Console.ReadKey();
            }
        }
    }
}

Sahara.cs:

namespace Sahara
{
    class Sahara
    {
        private static SaharaServer server;
        public static void Initialize()
        {
            server = new SaharaServer();
        }
        public static SaharaServer GetServer()
        {
            return server;
        }
    }
}
SaharaServer:
using Sahara.Core.Config;
using Sahara.Core.Logging;
using Sahara.Core.Server;
using System;
using System.Diagnostics;
namespace Sahara
{
    class SaharaServer
    {
        private readonly ServerStatusUpdater serverStatusUpdater;
        private readonly LogManager logManager;
        private readonly ServerInformation serverInformation;
        private readonly DateTime startedTime;
        private readonly ConfigManager configManager;
        public SaharaServer()
        {
            logManager = new LogManager();
            serverInformation = new ServerInformation();
            foreach (string consoleOutputString in serverInformation.ConsoleLogo)
            {
                Console.WriteLine(consoleOutputString);
            }
            logManager.Log("Loading " + serverInformation.ServerName + "...", LogType.Information);
            Stopwatch stopwatch = Stopwatch.StartNew();
            configManager = new ConfigManager("Extra/Other/config.ini");
            startedTime = DateTime.Now;
            serverStatusUpdater = new ServerStatusUpdater();
            stopwatch.Stop();
            logManager.Log("Finished Loading! [" + stopwatch.ElapsedMilliseconds + "ms]", LogType.Warning);
            Console.ForegroundColor = ConsoleColor.Black;
        }
        public LogManager GetLogManager()
        {
            return logManager;
        }
        public ServerInformation GetServerInformation()
        {
            return serverInformation;
        }
        public DateTime StartedTime
        {
            get { return startedTime; }
        }
        public ConfigManager GetConfigManager()
        {
            return configManager;
        }
        public void Dispose()
        {
            try
            {
                serverStatusUpdater.Dispose();
            }
            catch (Exception exception)
            {
                if (logManager != null)
                {
                    logManager.Log("Error in disposing SaharaServer: " + exception.Message, LogType.Error);
                    logManager.Log(exception.StackTrace, LogType.Error);
                }
            }
            finally
            {
                Environment.Exit(0);
            }
        }
    }
}

但是为什么GetServer()在Sahara.cs中返回null! ?

方法GetServer()返回null,原因未知

您从SaharaServer构造函数中调用ConfigManager构造函数,因此构造函数尚未完成设置server字段,因此GetServer将返回null