模仿Python字典语法的C#方法 [英] C# way to mimic Python Dictionary Syntax

查看:297
本文介绍了模仿Python字典语法的C#方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#中是否有一种很好的方式来模仿以下python语法:

Is there a good way in C# to mimic the following python syntax:

mydict = {}
mydict["bc"] = {}
mydict["bc"]["de"] = "123";  # <-- This line
mydict["te"] = "5";          # <-- While also allowing this line

换句话说,我想使用[ ]样式访问,可以根据设置方式返回其他字典或字符串类型。

In other words, I'd like something with [] style access that can return either another dictionary or a string type, depending on how it has been set.

我一直在尝试使用自定义类但似乎无法成功。有想法吗?

I've been trying to work this out with a custom class but can't seem to succeed. Any ideas?

谢谢!

编辑:我知道我是邪恶的。 Jared Par的解决方案很棒。 。 。对于这种形式的2级字典。但是,我也对进一步的水平感到好奇。 。 。例如,

I'm being evil, I know. Jared Par's solution is great . . . for a 2-level dictionary of this form. However, I am also curious about further levels . . . for instance,

mydict["bc"]["df"]["ic"] = "32";

以此类推。有什么想法吗?

And so on. Any ideas about that?

编辑3:

这是我最后使用的最后一堂课:

Here is the final class I ended up using:

class PythonDict {
    /* Public properties and conversions */
    public PythonDict this[String index] {
        get {
            return this.dict_[index];
        }
        set {
            this.dict_[index] = value;
        }
    }

    public static implicit operator PythonDict(String value) {
        return new PythonDict(value);
    }

    public static implicit operator String(PythonDict value) {
        return value.str_;
    }

    /* Public methods */
    public PythonDict() {
        this.dict_ = new Dictionary<String, PythonDict>();
    }

    public PythonDict(String value) {
        this.str_ = value;
    }

    public bool isString() {
        return (this.str_ != null);
    }

    /* Private fields */
    Dictionary<String, PythonDict> dict_ = null;
    String str_ = null;
}

该类适用于无限级,无需显式转换即可读取(

This class works for infinite levels, and can be read from without explicit conversion (dangerous, maybe, but hey).

用法如下:

        PythonDict s = new PythonDict();
        s["Hello"] = new PythonDict();
        s["Hello"]["32"] = "hey there";
        s["Hello"]["34"] = new PythonDict();
        s["Hello"]["34"]["Section"] = "Your face";
        String result = s["Hello"]["34"]["Section"];
        s["Hi there"] = "hey";

非常感谢Jared Par!

Thank you very much Jared Par!

推荐答案

您可以通过使该类(称为PythonDictionary)来实现此目的,该类是从 mydict [ bc] 返回的以下成员。

You can achieve this by having the class, lets call it PythonDictionary, which is returned from mydict["bc"] have the following members.


  • 允许[[de]]访问的索引器属性

  • A从字符串隐式转换为PythonDictionary

这应该允许两种情况都可以编译。

That should allow both cases to compile just fine.

例如

public class PythonDictionary {
    public string this[string index] {
        get { ... }
        set { ... }
    }
    public static implicit operator PythonDictionary(string value) {
        ...
    }
}

public void Example() {
    Dictionary<string, PythonDictionary> map = new Dictionary<string, PythonDictionary>();
    map["42"]["de"] = "foo";
    map["42"] = "bar";
}

这篇关于模仿Python字典语法的C#方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆