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

C#模仿Python字典语法的方法,c#,python,syntax,dictionary,types,C#,Python,Syntax,Dictionary,Types,在C#中有没有一种模仿以下python语法的好方法: mydict = {} mydict["bc"] = {} mydict["bc"]["de"] = "123"; # <-- This line mydict["te"] = "5"; # <-- While also allowing this line 等等。有什么想法吗 编辑3: 这是我最终使用的最后一个类: class PythonDict { /* Public properties an

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

mydict = {}
mydict["bc"] = {}
mydict["bc"]["de"] = "123";  # <-- This line
mydict["te"] = "5";          # <-- While also allowing this line
等等。有什么想法吗

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

非常感谢你,Jared Par! 您可以通过让类名为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";
}
公共类PythonDictionary{
公共字符串此[字符串索引]{
获取{…}
集合{…}
}
公共静态隐式运算符PythonDictionary(字符串值){
...
}
}
公共无效示例(){
字典映射=新字典();
地图[“42”][“de”]=“foo”;
地图[“42”]=“条形图”;
}

感谢您发布此问题和解决方案。已转换为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"

做得很好。我讨厌这些隐式转换,但它确实可以让你做OP想要做的事情。一个或两个键的伟大解决方案。我喜欢它,它可能会在我的情况下工作(所以非常感谢!)。然而,我也对更深层次的词典感到好奇。有什么想法吗?是的,这是非常可扩展的。结果类显示在问题中。非常感谢你!这个答案不太符合政府的政策(并被标记为这样),因为它没有回答OP的问题(关于C),但我认为它增加了价值。我建议不要删除它。但是,请注意,寻找纯VB解决方案的人可能不会想检查这个问题。只是想做出贡献。@jmjonson85我尊重这一点。我在评论队列中读到了你的答案,因为它被标记了。可以说,我只是希望让你了解情况。
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"