C# 模拟 Python 字典语法的方法
有没有什么好的方法可以在C#中模仿下面的Python语法:
mydict = {}
mydict["bc"] = {}
mydict["bc"]["de"] = "123"; # <-- This line
mydict["te"] = "5"; # <-- While also allowing this line
换句话说,我想要一种可以用方括号([])访问的方式,它可以根据设置返回另一个字典或者字符串类型。
我一直在尝试用自定义类来实现这个功能,但似乎没有成功。有没有什么好主意?
谢谢!
编辑:我知道这样做有点不太好。Jared Par的解决方案很好……适用于这种形式的两层字典。不过,我对更多层次的实现也很感兴趣……比如说,
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 个回答
2
感谢你提出这个问题和解决方案。下面是转换成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"
14
你可以通过创建一个类,咱们叫它 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";
}