FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ansys-pythonnet/src/runtime/Codecs/IterableDecoder.cs at update-python · ansys/ansys-pythonnet · GitHub
ansys
ansys-pythonnet
Repository navigation
Code
Issues
3
(3)
Pull requests
4
(4)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ansys-pythonnet
/
src
/
runtime
/
Codecs
/
IterableDecoder.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
55 lines (45 loc) · 1.78 KB
Breadcrumbs
ansys-pythonnet
/
src
/
runtime
/
Codecs
/
IterableDecoder.cs
Copy path
File metadata and controls
55 lines (45 loc) · 1.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using
System
;
using
System
.
Collections
.
Generic
;
namespace
Python
.
Runtime
.
Codecs
{
public
class
IterableDecoder
:
IPyObjectDecoder
{
internal
static
bool
IsIterable
(
Type
targetType
)
{
//if it is a plain IEnumerable, we can decode it using sequence protocol.
if
(
targetType
==
typeof
(
System
.
Collections
.
IEnumerable
)
)
return
true
;
if
(
!
targetType
.
IsGenericType
)
return
false
;
return
targetType
.
GetGenericTypeDefinition
(
)
==
typeof
(
IEnumerable
<
>
)
;
}
internal
static
bool
IsIterable
(
PyType
objectType
)
{
return
objectType
.
HasAttr
(
"__iter__"
)
;
}
public
bool
CanDecode
(
PyType
objectType
,
Type
targetType
)
{
return
IsIterable
(
objectType
)
&&
IsIterable
(
targetType
)
;
}
public
bool
TryDecode
<
T
>
(
PyObject
pyObj
,
out
T
value
)
{
//first see if T is a plan IEnumerable
if
(
typeof
(
T
)
==
typeof
(
System
.
Collections
.
IEnumerable
)
)
{
object
enumerable
=
new
CollectionWrappers
.
IterableWrapper
<
object
>
(
pyObj
)
;
value
=
(
T
)
enumerable
;
return
true
;
}
var
elementType
=
typeof
(
T
)
.
GetGenericArguments
(
)
[
0
]
;
var
collectionType
=
typeof
(
CollectionWrappers
.
IterableWrapper
<
>
)
.
MakeGenericType
(
elementType
)
;
var
instance
=
Activator
.
CreateInstance
(
collectionType
,
new
[
]
{
pyObj
}
)
;
value
=
(
T
)
instance
;
return
true
;
}
public
static
IterableDecoder
Instance
{
get
;
}
=
new
IterableDecoder
(
)
;
public
static
void
Register
(
)
{
PyObjectConversions
.
RegisterDecoder
(
Instance
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL