C#模仿Python字典语法的方式

时间:2009-09-04 20:21:21

标签: c# python syntax dictionary types

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

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

换句话说,我喜欢[]样式访问的东西,可以返回另一个字典或字符串类型,具体取决于它的设置方式。

我一直试图用自定义类来解决这个问题,但似乎无法成功。有什么想法吗?

谢谢!

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

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

等等。有关于此的任何想法吗?

编辑3:

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

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

这个类适用于无限级别,可以在没有显式转换的情况下读取(危险,可能,但是嘿)。

这样的用法:

        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!

2 个答案:

答案 0 :(得分:12)

你可以通过让类来实现这一点,让我们称之为PythonDictionary,从mydict["bc"]返回的有以下成员。

  • 允许[“de”]访问的索引器属性
  • 从字符串到PythonDictionary的隐式转换

这应该允许两种情况编译得很好。

例如

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

答案 1 :(得分:1)

感谢您发布此问题和解决方案。转换为VB.NET:

Public Class PythonDict
    ' Public properties and conversions
    Default Public Property Item(ByVal index As String) As PythonDict
        Get
            Return Me.dict_(index)
        End Get

       Set(value As PythonDict)
            Me.dict_(index) = value
       End Set
    End Property

    Public Shared Narrowing Operator CType(value As String) As PythonDict
       Return New PythonDict(value)
    End Operator

    Public Shared Widening Operator CType(value As PythonDict) As String
       Return value.str_
    End Operator

    ' Public methods
    Public Sub New()
       Me.dict_ = New Dictionary(Of String, PythonDict)()
    End Sub

    Public Sub New(value As String)
       Me.str_ = value
    End Sub

    Public Function isString() As Boolean
       Return (Me.str_ IsNot Nothing)
    End Function

    ' Private fields
    Private dict_ As Dictionary(Of String, PythonDict) = Nothing
    Private str_ As String = Nothing
End Class

用法:

Dim s As PythonDict = New PythonDict()
s("Hello") = New PythonDict()
s("Hello")("32") = "hey there"
s("Hello")("34") = New PythonDict()
s("Hello")("34")("Section") = "Your face"
Dim result As String = s("Hello")("34")("Section")
s("Hi there") = "hey"