设置后静态上下文为null

问题描述

此.Net Core 3.1(NUnit 3)测试在Visual Studio 2019下工作。

但是一旦我使用JetBrains Rider,测试就会失败,因为静态属性(DataA)在大多数情况下都会返回null

有什么主意,怎么解决这个问题?

public static class ClassA
{
    public static string DataA { get; set; }
    public static string DataB { get; set; }
    
    public static void SetAppConfig(string appSettingsPfad)
    {
        IList<JProperty> appSettings = JsonConvert.DeserializeObject<JToken>(appSettingsPfad).Select(j => (JProperty)j).ToList();

        var appKonfiguration = typeof(ClassA).GetProperties(BindingFlags.Public | BindingFlags.Static);

        foreach (JProperty property in appSettings)
        {
            var appKonfigurationProperty = appKonfiguration.SingleOrDefault(p => p.Name.Equals(property.Name,StringComparison.OrdinalIgnoreCase));
            var value = ((JValue)property.Value).Value;

            if (appKonfigurationProperty != null)
                appKonfigurationProperty.SetValue(null,Convert.ChangeType(value,appKonfigurationProperty.PropertyType));
        }
    }
}

[TestFixture]
public class DummyTest
{
    [Test]
    public void TestA()
    {
        LoadAppConfig();
        
        // DataA returns null here
        Console.WriteLine(ClassA.DataA);
        
        new BusinessLogicClass.DoSomething();
        // [...]
    }
    
    private void LoadAppConfig() {
        // [...]
        string appSettings = File.ReadAllText(jsonConfigFiles.Single());
        ClassA.SetAppConfig(appSettings);
        
        // here it works and a result is the proper string
        Console.WriteLine(ClassA.DataA);
        // [...]
    }
}

public class BusinessLogicClass {
    
    public void DoSomething() {
        // DataA returns null here
        Console.WriteLine(ClassA.DataA);
        // [...]
    }
}

(我已将此代码简化到最低限度,原代码将在进行空检查后运行为Exception)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...