C# 如何将对象的键作为键值对获取?

C# 如何将对象的键作为键值对获取?,c#,.net-core,C#,.net Core,我有一个类对象,我需要以特定的格式存储它的值和键 public class AppSettings { public int TokenLifeTime { get; set; } = 450; public List<string> Urls { get; set; } = new List<string> { "www.google.com", "www.hotmail.com" }; pub

我有一个类对象,我需要以特定的格式存储它的值和键

public class AppSettings
{
    public int TokenLifeTime { get; set; } = 450;

    public List<string> Urls { get; set; } = new List<string>
    {
        "www.google.com",
        "www.hotmail.com"
    };

    public List<ServersList> ServersList { get; set; } = new List<ServersList>
    {
        new ServersList {IsHttpsAllowed = false},
        new ServersList {IsHttpsAllowed = true}
    };
}

public class ServersList
{
    public bool IsHttpsAllowed { get; set; }
}

是否有任何方法可以递归地将所有键作为字符串获取,而不考虑对象深度。上面的代码只是一个实例,我有一个很长的列表和更多的数据。

我有几个示例供您参考:

备选案文1:

public class AppSettings
{
    public int TokenLifeTime { get; set; } = 450;

    public Dictionary<string, ServersList> Urls { get; set; } = new Dictionary<string, ServersList>
    {
        {"www.google.com", new ServersList {IsHttpsAllowed = false}},
        { "www.hotmail.com", new ServersList {IsHttpsAllowed = true}}

    };
}

public class ServersList
{
    public bool IsHttpsAllowed { get; set; }
}
公共类AppSettings
{
公共int令牌生命周期{get;set;}=450;
公共字典URL{get;set;}=新字典
{
{“www.google.com”,新服务器列表{IsHttpsAllowed=false},
{“www.hotmail.com”,新服务器列表{IsHttpsAllowed=true}
};
}
公共类服务器列表
{
公共bool ISHTTPS允许{get;set;}
}
此选项会将值分组在一起,但会丢失基于“int”的索引。不确定这是否重要

备选案文2:

 public class AppSettings
{
    public int TokenLifeTime { get; set; } = 450;

    public List<KeyValuePair<string, ServersList>> Urls { get; set; } = new List<KeyValuePair<string, ServersList>>
    {
        new KeyValuePair<string, ServersList>("www.google.com",new ServersList {IsHttpsAllowed = false}),
        new KeyValuePair<string, ServersList>("www.hotmail.com", new ServersList {IsHttpsAllowed = true})
    };

    public List<ServersList> ServersList { get; set; } = new List<ServersList>
    {
        new ServersList {IsHttpsAllowed = false},
        new ServersList {IsHttpsAllowed = true}
    };
}

public class ServersList
{
    public bool IsHttpsAllowed { get; set; }
}
公共类AppSettings
{
公共int令牌生命周期{get;set;}=450;
公共列表URL{get;set;}=新列表
{
新的KeyValuePair(“www.google.com”,新服务器列表{IsHttpsAllowed=false}),
新的KeyValuePair(“www.hotmail.com”,新服务器列表{IsHttpsAllowed=true})
};
公共列表服务器列表{get;set;}=新列表
{
新服务器列表{ISHTTPSSallowed=false},
新服务器列表{ISHTTPSSallowed=true}
};
}
公共类服务器列表
{
公共bool ISHTTPS允许{get;set;}
}
此选项将保留基于“int”的索引,并且值仍然分组。但感觉笨重

选项3:(我会选择的那个)

公共类AppSettings
{
公共int令牌生命周期{get;set;}=450;
公共列表服务器列表{get;set;}=新列表
{
新服务器{Url=“www.google.com”,IsHttpsAllowed=false},
新服务器{Url=“www.hotmail.com”,IsHttpsAllowed=true}
};
}
公共类服务器
{
公共字符串Url{get;set;}
公共bool ISHTTPS允许{get;set;}
}

此选项仍然提供基于“int”的索引,并将数据分组在一起(正如我在示例中所理解的那样)。

我不认为有任何现成的方法

你需要自己创造一些东西并定义你的规则

在其更原始的形式中,我将从以下内容开始:

      Type t = typeof(AppSettings);
      Console.WriteLine("The {0} type has the following properties: ",
                        t.Name);
      foreach (var prop in t.GetProperties())
         Console.WriteLine("   {0} ({1})", prop.Name,
                           prop.PropertyType.Name);

然后为
IEnumerable
添加一条规则,以便在迭代中处理对象和基元值类型的属性。

我建议您为类属性指定适当的名称,这样它们的用途会更清楚(这将在调试期间为您节省大量问题)。因为读了这篇文章,我不知道什么应该是你的“密钥”。老实说,我对堆栈溢出保持了简单,否则密钥会更长。我认为简化名称会有很大帮助。也许为这两个参数添加构造函数是个好主意。还可以/应该为每个“ServerList”项指定“TokenLifeTime”。但这不是我所要求的。我可以重新安排类并对它们进行分组,但提供的代码是为了给出不进行调整的想法。
public class AppSettings
{
    public int TokenLifeTime { get; set; } = 450;

    public List<Server> ServersList { get; set; } = new List<Server>
    {
        new Server { Url = "www.google.com", IsHttpsAllowed = false},
        new Server { Url = "www.hotmail.com", IsHttpsAllowed = true}
    };
}

public class Server
{
    public string Url { get; set; }
    public bool IsHttpsAllowed { get; set; }
}
      Type t = typeof(AppSettings);
      Console.WriteLine("The {0} type has the following properties: ",
                        t.Name);
      foreach (var prop in t.GetProperties())
         Console.WriteLine("   {0} ({1})", prop.Name,
                           prop.PropertyType.Name);