You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|**Python**|`List__T1` (import) or ``List`1 `` (load_type) |`puerts.generic(List, System.Int32)`|
117
+
|**Python**|`List`|`List[System.Int32]`|
118
118
119
-
> The backtick `` ` `` substitute differs across languages: JS uses `$`, Lua uses `_`, Python import uses `__T`.
119
+
> In Python, using `import XXX` imports a special generic factory type. When called with type arguments, it creates an instance of a generic type based on the number of mixed-in type arguments.
C# `[]` operators (arrays, `List`, `Dictionary`, custom indexers) **cannot** be accessed directly with `[]` in these three languages. You must use `get_Item()` / `set_Item()`.
> ⚠️ Lua uses colon `:` syntax for instance methods, while JS and Python use dot `.` syntax. This rule applies to all C# types with indexers (arrays, `List`, `Dictionary`, etc.).
| Generic type |`puer.$generic()`|`puerts.generic()`|You can import the generic directly using `import` (e.g., `import List`). For details, refer to the explanation of generics above.|
PuerTS extends Python's `import` mechanism to directly import C# namespaces and types. You can use standard Python `import` / `from ... import` / `import ... as` syntax.
52
52
53
-
**Method 2: Using `puerts.load_type()` for dynamic loading**
`puerts.load_type()` accepts a full type name string, which is useful when dynamically loading types or when the name contains special characters (such as the `+` sign for nested types).
70
-
71
53
> ⚠️ **Note**: Accessing a non-existent namespace or type will throw a `ModuleNotFoundError` exception.
# use square bracket syntax to create generic type: List<int>
169
+
List_Int32 = List[Int32]
170
+
ls = List_Int32()
189
171
ls.Add(1)
190
172
ls.Add(2)
191
173
ls.Add(3)
@@ -197,20 +179,20 @@ print(ls.Count) # 3
197
179
}
198
180
```
199
181
200
-
> ⚠️ **Special naming for generic types**: In C#, generic type names use backticks to indicate the number of type parameters (e.g. `` List`1 ``). In Python's `import` syntax, backticks must be replaced with `_` followed by the count:
201
-
> -`` List`1 `` → `List_1`
202
-
> -`` Dictionary`2 `` → `Dictionary_2`
203
-
>
204
-
> When using `puerts.load_type()`, you can use the original backtick format directly:
205
-
> ```python
206
-
> List = puerts.load_type('System.Collections.Generic.List`1')
207
-
>```
182
+
The following import syntaxes are equivalent:
183
+
184
+
```python
185
+
import System.Collections.Generic.List as List
186
+
from System.Collections.Generic import List
187
+
```
188
+
189
+
You can also directly use syntax like `List[System.String]` to create a generic type.
190
+
191
+
For multiple type arguments, separate them with commas: `Dictionary[String, Int32]`.
C# `[]` operators (including array indexing, `List` indexing, `Dictionary` indexing, and any custom indexer) **cannot** be accessed directly with Python `[]` syntax for C# objects. You must use `get_Item()` / `set_Item()`:
239
227
240
-
// use load_type for nested types (with'+' separator)
d.set_Item('key', 100) # equivalent to C#: dict['key'] = 100
253
+
v = d.get_Item('key') # equivalent to C#: v = dict['key']
245
254
''')
246
255
");
256
+
env.Dispose();
257
+
}
247
258
```
248
259
249
-
>💡 When using `puerts.load_type()` to access nested types, use the C#reflection `+` separator format (e.g. `OuterClass+InnerClass`).
260
+
> ⚠️ **Important**: Although Python native `[]` is used for Python lists/dicts, index access on C# objects must use `get_Item()` / `set_Item()`. This is consistent with JS and Lua rules.
250
261
251
262
----------------------------
252
263
### typeof
@@ -400,6 +411,40 @@ void Start() {
400
411
}
401
412
```
402
413
414
+
----------------------------
415
+
### Iterators
416
+
417
+
Use `puerts.gen_iterator()` to convert C# iterable objects (objects implementing `IEnumerable`) into Python iterators, so you can use Python `for ... in` loops:
`puerts.gen_iterator()` internally calls the object's `GetEnumerator()` method and wraps it as an iterator object that supports Python iteration protocol (`__iter__` / `__next__`).
447
+
403
448
-------------
404
449
This section covers calling C# from Python. In the next section, we'll go the other direction: [Invoking Python from C#](./cs2python.md).
0 commit comments