C#: How to make sure a settings variable exists before attempting to use it from another assembly?

前端 未结 5 1985
忘了有多久
忘了有多久 2021-02-07 11:10

I have the following:

using CommonSettings = MyProject.Commons.Settings;

public class Foo
{
    public static void DoSomething(string str)
    {
        //How d         


        
5条回答
  •  耶瑟儿~
    2021-02-07 11:49

    Depending on what type CommomSettings.Default is, a simple null check should be fine:

    if(setting != null)
        DoSomethingElse(setting);
    

    If you want to check BEFORE trying to retrieve the setting, you need to post the Type of CommonSettings.Default. It looks like a Dictionary so you might be able to get away with:

    if(CommonSettings.Default.ContainsKey(str))
    {
        DoSomethingElse(CommonSettings.Default[str]);
    }
    

提交回复
热议问题