简体   繁体   中英

How to get keys of objects as key value pairs?

I have class object and I need to store its values and keys in specific format.

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; }
}

I want to get keys in this format.

"AppSettings:TokenLifeTime" , 450
"AppSettings:Urls:0", "www.google.com"
"AppSettings:Urls:1", "www.hotmail.com"
"AppSettings:ServersList:0:IsHttpsAllowed", false
"AppSettings:ServersList:1:IsHttpsAllowed", true

Is there any way to get all keys as string recursively regardless of object depths. Above code is just an example in real case I have long list and lot more data.

I don't think that there is anything out of the box for this.

You would need to create something yourself and define your rules.

In its more primitive form, I'd start with this:

      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);

Then add a rule for IEnumerable to handle them in iterations and so forth for objects and primitive value types.

I have a couple of examples for you:

Option 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; }
}

This option would group the values together but you would lose 'int' based index. Not sure if that is important.

Option 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; }
}

This option will retain 'int' based indexing and the values are still grouped. But feels clunky...

Option 3: (the one I would go with)

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; }
}

This option still gives you 'int' based indexing and it groups the data together (as it should be from what I understand in the example).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM