FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnet/src/runtime/StateSerialization/MaybeMethodBase.cs at master · JakeJP/pythonnet · GitHub
JakeJP
/
pythonnet
Public
forked from
pythonnet/pythonnet
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnet
/
src
/
runtime
/
StateSerialization
/
MaybeMethodBase.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
185 lines (155 loc) · 6.71 KB
Breadcrumbs
pythonnet
/
src
/
runtime
/
StateSerialization
/
MaybeMethodBase.cs
Copy path
File metadata and controls
185 lines (155 loc) · 6.71 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using
System
;
using
System
.
Diagnostics
;
using
System
.
Diagnostics
.
CodeAnalysis
;
using
System
.
Reflection
;
using
System
.
Runtime
.
Serialization
;
using
System
.
Linq
;
using
Python
.
Runtime
.
Reflection
;
namespace
Python
.
Runtime
{
[
Serializable
]
internal
struct
MaybeMethodBase
<
T
>
:
ISerializable
where
T
:
MethodBase
{
// .ToString() of the serialized object
const
string
SerializationName
=
"s"
;
// The ReflectedType of the object
const
string
SerializationType
=
"t"
;
// Fhe parameters of the MethodBase
const
string
SerializationParameters
=
"p"
;
const
string
SerializationMethodName
=
"n"
;
const
string
SerializationGenericParamCount
=
"G"
;
const
string
SerializationFlags
=
"V"
;
public
static
implicit
operator
MaybeMethodBase
<
T
>
(
T
?
ob
)
=>
new
(
ob
)
;
readonly
string
?
name
;
readonly
MethodBase
?
info
;
[
NonSerialized
]
readonly
Exception
?
deserializationException
;
public
string
DeletedMessage
{
get
{
return
$
"The .NET
{
typeof
(
T
)
}
{
name
}
no longer exists. Cause: "
+
deserializationException
?
.
Message
;
}
}
public
T
Value
{
get
{
if
(
info
==
null
)
{
throw
new
SerializationException
(
DeletedMessage
,
innerException
:
deserializationException
)
;
}
return
(
T
)
info
;
}
}
public
T
UnsafeValue
=>
(
T
)
info
!
;
public
string
?
Name
=>
name
;
[
MemberNotNullWhen
(
true
,
nameof
(
info
)
)
]
public
bool
Valid
=>
info
!=
null
;
public
override
string
ToString
(
)
{
return
(
info
!=
null
?
info
.
ToString
(
)
:
$
"missing method info:
{
name
}
"
)
;
}
public
MaybeMethodBase
(
T
?
mi
)
{
info
=
mi
;
name
=
mi
?
.
ToString
(
)
;
Debug
.
Assert
(
name
!=
null
||
info
==
null
)
;
deserializationException
=
null
;
}
internal
MaybeMethodBase
(
SerializationInfo
serializationInfo
,
StreamingContext
context
)
{
name
=
serializationInfo
.
GetString
(
SerializationName
)
;
info
=
null
;
deserializationException
=
null
;
if
(
name
is
null
)
return
;
try
{
// Retrieve the reflected type of the method;
var
typeName
=
serializationInfo
.
GetString
(
SerializationType
)
;
var
tp
=
Type
.
GetType
(
typeName
)
;
if
(
tp
==
null
)
{
throw
new
SerializationException
(
$
"The underlying type
{
typeName
}
can't be found"
)
;
}
var
flags
=
(
MaybeMethodFlags
)
serializationInfo
.
GetInt32
(
SerializationFlags
)
;
int
genericCount
=
serializationInfo
.
GetInt32
(
SerializationGenericParamCount
)
;
// Get the method's parameters types
var
field_name
=
serializationInfo
.
GetString
(
SerializationMethodName
)
;
var
param
=
(
ParameterHelper
[
]
)
serializationInfo
.
GetValue
(
SerializationParameters
,
typeof
(
ParameterHelper
[
]
)
)
;
info
=
ScanForMethod
(
tp
,
field_name
,
genericCount
,
flags
,
param
)
;
}
catch
(
Exception
e
)
{
deserializationException
=
e
;
}
}
static
MethodBase
ScanForMethod
(
Type
declaringType
,
string
name
,
int
genericCount
,
MaybeMethodFlags
flags
,
ParameterHelper
[
]
parameters
)
{
var
bindingFlags
=
ClassManager
.
BindingFlags
;
if
(
flags
.
HasFlag
(
MaybeMethodFlags
.
Constructor
)
)
bindingFlags
&=
~
BindingFlags
.
Static
;
var
alternatives
=
declaringType
.
GetMember
(
name
,
flags
.
HasFlag
(
MaybeMethodFlags
.
Constructor
)
?
MemberTypes
.
Constructor
:
MemberTypes
.
Method
,
bindingFlags
)
;
if
(
alternatives
.
Length
==
0
)
throw
new
MissingMethodException
(
$
"
{
declaringType
}
.
{
name
}
"
)
;
var
visibility
=
flags
&
MaybeMethodFlags
.
Visibility
;
var
result
=
alternatives
.
Cast
<
MethodBase
>
(
)
.
FirstOrDefault
(
m
=>
MatchesGenericCount
(
m
,
genericCount
)
&&
MatchesSignature
(
m
,
parameters
)
&&
(
Visibility
(
m
)
==
visibility
||
ClassManager
.
ShouldBindMethod
(
m
)
)
)
;
if
(
result
is
null
)
throw
new
MissingMethodException
(
$
"Matching overload not found for
{
declaringType
}
.
{
name
}
"
)
;
return
result
;
}
static
bool
MatchesGenericCount
(
MethodBase
method
,
int
genericCount
)
=>
method
.
ContainsGenericParameters
?
method
.
GetGenericArguments
(
)
.
Length
==
genericCount
:
genericCount
==
0
;
static
bool
MatchesSignature
(
MethodBase
method
,
ParameterHelper
[
]
parameters
)
{
var
curr
=
method
.
GetParameters
(
)
;
if
(
curr
.
Length
!=
parameters
.
Length
)
return
false
;
for
(
int
i
=
0
;
i
<
curr
.
Length
;
i
++
)
if
(
!
parameters
[
i
]
.
Matches
(
curr
[
i
]
)
)
return
false
;
return
true
;
}
public
void
GetObjectData
(
SerializationInfo
serializationInfo
,
StreamingContext
context
)
{
serializationInfo
.
AddValue
(
SerializationName
,
name
)
;
if
(
Valid
)
{
serializationInfo
.
AddValue
(
SerializationMethodName
,
info
.
Name
)
;
serializationInfo
.
AddValue
(
SerializationGenericParamCount
,
info
.
ContainsGenericParameters
?
info
.
GetGenericArguments
(
)
.
Length
:
0
)
;
serializationInfo
.
AddValue
(
SerializationFlags
,
(
int
)
Flags
(
info
)
)
;
string
?
typeName
=
info
.
ReflectedType
.
AssemblyQualifiedName
;
Debug
.
Assert
(
typeName
!=
null
)
;
serializationInfo
.
AddValue
(
SerializationType
,
typeName
)
;
ParameterHelper
[
]
parameters
=
(
from
p
in
info
.
GetParameters
(
)
select
new
ParameterHelper
(
p
)
)
.
ToArray
(
)
;
serializationInfo
.
AddValue
(
SerializationParameters
,
parameters
,
typeof
(
ParameterHelper
[
]
)
)
;
}
}
static
MaybeMethodFlags
Flags
(
MethodBase
method
)
{
var
flags
=
MaybeMethodFlags
.
Default
;
if
(
method
.
IsConstructor
)
flags
|=
MaybeMethodFlags
.
Constructor
;
if
(
method
.
IsStatic
)
flags
|=
MaybeMethodFlags
.
Static
;
if
(
method
.
IsPublic
)
flags
|=
MaybeMethodFlags
.
Public
;
return
flags
;
}
static
MaybeMethodFlags
Visibility
(
MethodBase
method
)
=>
Flags
(
method
)
&
MaybeMethodFlags
.
Visibility
;
}
[
Flags
]
internal
enum
MaybeMethodFlags
{
Default
=
0
,
Constructor
=
1
,
Static
=
2
,
// TODO: other kinds of visibility
Public
=
32
,
Visibility
=
Public
,
}
}
Back
|
FazBrowse Home
|
New Git URL