【问题标题】:Getting value by name from OrderedDictionary [closed]从 OrderedDictionary 按名称获取价值 [关闭]
【发布时间】:2013-03-23 12:35:06
【问题描述】:

我正在寻找类似 PHP 的关联数组,它也支持嵌套。例如,我正在创建一个 Dictionary 对象,如下所示:

System.Collections.Specialized.OrderedDictionary userRoles = new System.Collections.Specialized.OrderedDictionary();
userRoles["UserId"] = "120202";
userRoles["UserName"] = "Jhon Doe";

// 2D array Like
userRoles["UserRoles"][0] = "CAN_EDIT_LIST";
userRoles["UserRoles"][1] = "CAN_EDIT_PAGE";

然后我想通过 KeyNames 而不是索引值来访问它们。有可能吗?

【问题讨论】:

    标签: c# ordereddictionary


    【解决方案1】:

    OrderedDictionary 使用对象作为键和值。

    要实现嵌套,只需将值设置为另一个字典即可:

    userRoles["UserRoles"] = new Dictionary();
    

    那么你可以使用:

    ((Dictionary())userRoles["UserRoles"])["MyKey"] = "My Value";
    

    【讨论】:

    • 谢谢,这正是我想要的。我认为 Looping thru 也不会成为问题。另外我的另一部分是如何按名称访问值并阅读它?
    猜你喜欢
    • 2013-07-25
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 2016-04-24
    • 2017-09-05
    • 1970-01-01
    相关资源
    最近更新 更多