首页 > 解决方案 > 如何检查类中的对象是否不为空

问题描述

我有一个配置文件,其中包含有关不同数据库的详细信息,我正在阅读该文件,并且我为文件中的每个对象定义了模型(见下文)

我的配置文件:

{
"DatabaseSettings": {
          "Database": "db_Test",
          "Collection": "apptests",
          "Instance": "https://cosmos-tables.documents.azure.com:443/",
          "Key": "4c6ykE3Sutest=="
        },
        "TableSettings": {
          "AzureTable": {
            "Account": "testables",
            "Table": "atest",
            "Key": "test===="
          }
}

我的代码模型:

public class CosmosDbSettings
    {
        public string Database { get; set; }
        public string Collection { get; set; }
        public string Instance { get; set; }
        public string Key { get; set; }
    }
     public class AzureTableSettings
        {
            public string Account { get; set; }
            public string Key { get; set; }
            public string Table { get; set; }
        }
  public class MyClass
    {
        public AzureTableSettings TableSettings { get; set; }
        public CosmosDbSettings DatabaseSettings { get; set; }
}

现在我想检查我的 C# 方法,如果我在 CosmosDbSettings 类中的对象不为空,即我正在检查我的配置文件是否有 DatabseSettings 条目并且它们不为空或为空。我怎样才能检查这个?一种方法是检查每个对象是否 NullorEmpty() 但我相信有更好的方法可以做到这一点,我正在寻找一些帮助。

谢谢

标签: c#

解决方案


推荐阅读