我在使用JSON作为配置时遇到了一些问题(目前无法让我的用户使用此程序使用Config.config或Program.exe.config,并且JSON更容易请阅读我的意见。所以这是代码:
public static Bot bot = JsonConvert.DeserializeObject<Bot>(File.ReadAllText("Bot.config"));
public static string nick = bot.configuration.BotName;
返回string nick,这是我运行程序时遇到的错误:
未处理的异常:System.TypeInitializationException:类型初始值设定项 r'Bot.Program'抛出异常。 ---&GT; System.NullReferenceException:Objec t引用未设置为对象的实例。 在Program.cs中的Bot.Program..cctor():第38行 ---内部异常堆栈跟踪结束--- 在Bot.Program.Main(String [] args)
这就是我一直遇到的问题。
类:
public class Bot
{
public Configuration configuration { get; set; }
public Welcomesystem welcomeSystem { get; set; }
public Twittersystem twitterSystem { get; set; }
public Youtubesystem youtubeSystem { get; set; }
public Weathersystem weatherSystem { get; set; }
public Loyaltysystem loyaltySystem { get; set; }
public Urldetection urlDetection { get; set; }
public Commands commands { get; set; }
}
public class Configuration
{
public string BotName { get; set; }
public string Channel { get; set; }
public string Server { get; set; }
public string OAuth { get; set; }
public string BotAdmin { get; set; }
public string _comment1 { get; set; }
}
public class Welcomesystem
{
public bool enabled { get; set; }
public bool forSubsOnly { get; set; }
public bool forRegOnly { get; set; }
public string DefaultWelcome { get; set; }
}
public class Twittersystem
{
public bool enabled { get; set; }
public string _comment2 { get; set; }
public string APIKey { get; set; }
public string APISecret { get; set; }
public string TokenKey { get; set; }
public string TokenSecret { get; set; }
}
public class Youtubesystem
{
public bool enabled { get; set; }
}
public class Weathersystem
{
public bool enabled { get; set; }
public string authKey { get; set; }
public string _comment3 { get; set; }
}
public class Loyaltysystem
{
public bool enabled { get; set; }
public string _comment4 { get; set; }
}
public class Urldetection
{
public bool enabled { get; set; }
public string defaultTimeout { get; set; }
public string _comment5 { get; set; }
}
public class Commands
{
public bool Diablo_Command { get; set; }
public bool Warcraft_Command { get; set; }
}
JSON文件:
{
"Bot": {
"configuration": {
"BotName": "Trub0t",
"Channel": " ",
"Server": " ",
"OAuth": " ",
"BotAdmin": " ",
"_comment1": "You can get your oauth code for your bot at http://twitchapps.com/tmi/"
},
"welcomeSystem": {
"enabled": false,
"forSubsOnly": false,
"forRegOnly": false,
"DefaultWelcome": "-hugs @nick- Welcome to the stream, @upnick!"
},
"twitterSystem": {
"enabled": true,
"_comment2": "You need to get your own auth code for this",
"APIKey": "",
"APISecret": "",
"TokenKey": "",
"TokenSecret": ""
},
"youtubeSystem": {
"enabled": true
},
"weatherSystem": {
"enabled": true,
"authKey": "",
"_comment3": "You need your own Auth from WeatherUnderground API as well."
},
"loyaltySystem": {
"enabled": false,
"_comment4": "This feature isn't implemented yet."
},
"urlDetection": {
"enabled": false,
"defaultTimeout": "600",
"_comment5": "This feature isn't implemented yet."
},
"commands": {
"Diablo_Command": false,
"Warcraft_Command": false
}
}
}
答案 0 :(得分:0)
我暂时不再使用SQLite数据绑定和Visual Studio 2013中的.settings文件。
当我遇到这个问题时,我使用的其他解决方案现在已被弃用,但我现在将此代码保留给需要它的任何人。
public string Server = ConfigurationSettings.AppSettings["Server"].ToString();
public string Port = ConfigurationSettings.AppSettings["Port"].ToString();
public string Channel = ConfigurationSettings.AppSettings["Channel"].ToString();
public string OAuth = ConfigurationSettings.AppSettings["OAuth"].ToString();
public string Interval = ConfigurationSettings.AppSettings["Interval"].ToString();
public string Currency = ConfigurationSettings.AppSettings["Currency"].ToString();
public string Nick = ConfigurationSettings.AppSettings["Nick"].ToString();
public string BaseAdmin = ConfigurationSettings.AppSettings["BaseAdmin"].ToString();
使用JSON作为配置会带来一些问题,其中之一就是它在启动时会滞后我的应用程序,编写它也会很困难。所以,如果您在这里,我建议您使用XML作为配置或.Settings,因为它们比使用JSON更可行,更快。