@@ -173,33 +173,7 @@ print('ret=' + str(ret) + ', out=' + str(outRef[0]) + ', ref=' + str(refRef[0]))
173173----------------------------
174174### 泛型
175175
176- Python 中使用 ` puerts.generic() ` 来创建泛型类型:
177-
178- ``` csharp
179- void Start () {
180- var env = new Puerts .ScriptEnv (new Puerts .BackendPython ());
181- env .Eval (@"
182- exec('''
183- import System
184- import System.Collections.Generic.List_1 as List
185-
186- # create generic type: List<int>
187- ListInt = puerts.generic(List, System.Int32)
188- ls = ListInt()
189- ls.Add(1)
190- ls.Add(2)
191- ls.Add(3)
192-
193- print(ls.Count) # 3
194- ''')
195- " );
196- env .Dispose ();
197- }
198- ```
199-
200- ** 方式二:使用中括号语法(推荐)**
201-
202- PuerTS 还支持 Python 风格的中括号语法来实例化泛型类型,类似于 Python 标准库中 ` list[int] ` 的写法:
176+ PuerTS 支持 Python 风格的中括号语法来实例化泛型类型,类似于 Python 标准库中 ` list[int] ` 的写法:
203177
204178``` csharp
205179void Start () {
@@ -223,16 +197,22 @@ print(ls.Count) # 3
223197}
224198```
225199
226- 中括号语法 ` List_1[Int32] ` 等价于 ` puerts.generic(List_1, Int32) ` ,对于多个类型参数,使用逗号分隔:` Dictionary_2[String, Int32] ` 。
200+ 以下导入语法都是等价的
201+
202+ ``` python
203+ import System.Collections.Generic.List_1
204+ import System.Collections.Generic.List as List
205+ from System.Collections.Generic import List
206+ List_1 = puerts.load_type(' System.Collections.Generic.List`1' )
207+ ```
208+
209+ 同时也都可以直接使用 ` List[System.String] ` 语法来创建泛型类型
210+
211+ 对于多个类型参数,使用逗号分隔:` Dictionary_2[String, Int32] ` 。
227212
228213> ⚠️ ** 泛型类名的特殊表示** :C# 中泛型类名使用反引号表示类型参数个数(如 `` List`1 `` ),而在 Python 的 ` import ` 语法中,反引号需要替换为 ` _ ` 加参数个数:
229214> - `` List`1 `` → ` List_1 `
230215> - `` Dictionary`2 `` → ` Dictionary_2 `
231- >
232- > 使用 ` puerts.load_type() ` 时,可以直接使用反引号原始格式:
233- > ``` python
234- > List = puerts.load_type(' System.Collections.Generic.List`1' )
235- > ```
236216
237217----------------------------
238218### 嵌套类型
@@ -253,11 +233,11 @@ x = InnerClassA()
253233print(x.Foo) # Hello
254234
255235# access generic nested class
256- InnerClassB_1 = puerts.generic( TestNestedTypes.InnerClassB_1, Int32)
236+ InnerClassB_1 = TestNestedTypes.InnerClassB_1[ Int32]
257237y = InnerClassB_1()
258238print(y.Bar) # Hello
259239
260- InnerClassB_2 = puerts.generic( TestNestedTypes.InnerClassB_2, Int32, String)
240+ InnerClassB_2 = TestNestedTypes.InnerClassB_2[ Int32, String]
261241z = InnerClassB_2()
262242print(z.Bar) # Hello
263243''')
@@ -296,14 +276,14 @@ val = arr.get_Item(0) # 等价于 C# 的 val = arr[0]
296276print(val) # 42
297277
298278# 同样适用于 List<T>
299- ListInt = puerts.generic( List_1, System.Int32)
279+ ListInt = List_1[ System.Int32]
300280lst = ListInt()
301281lst.Add(10)
302282first = lst.get_Item(0) # 等价于 C# 的 lst[0]
303283lst.set_Item(0, 20) # 等价于 C# 的 lst[0] = 20
304284
305285# 同样适用于 Dictionary<TKey, TValue>
306- DictStrInt = puerts.generic( Dictionary_2, System.String, System.Int32)
286+ DictStrInt = Dictionary_2[ System.String, System.Int32]
307287d = DictStrInt()
308288d.set_Item('key', 100) # 等价于 C# 的 dict['key'] = 100
309289v = d.get_Item('key') # 等价于 C# 的 v = dict['key']
0 commit comments