FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SharpUtils/Serialization/ObjectConstructInfo.cs at master · IS4Code/SharpUtils · GitHub
IS4Code
/
SharpUtils
Public
Notifications
You must be signed in to change notification settings
Fork
13
Star
83
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
SharpUtils
/
Serialization
/
ObjectConstructInfo.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
108 lines (100 loc) · 2.84 KB
Breadcrumbs
SharpUtils
/
Serialization
/
ObjectConstructInfo.cs
Copy path
File metadata and controls
108 lines (100 loc) · 2.84 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
/* Date: 19.4.2015, Time: 15:54 */
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Collections
.
ObjectModel
;
using
System
.
Reflection
;
using
System
.
Runtime
.
Serialization
;
namespace
IllidanS4
.
SharpUtils
.
Serialization
{
[
Serializable
]
public
class
ObjectConstructInfo
{
private
static
readonly
ObjectIDGenerator
idGen
=
new
ObjectIDGenerator
(
)
;
public
Type
Type
{
get
;
private
set
;
}
public
ReadOnlyCollection
<
FieldInfo
>
Fields
{
get
;
private
set
;
}
public
ReadOnlyCollection
<
object
>
Data
{
get
;
private
set
;
}
public
long
Id
{
get
;
private
set
;
}
private
Dictionary
<
long
,
ObjectConstructInfo
>
objCache
=
new
Dictionary
<
long
,
ObjectConstructInfo
>
(
)
;
public
ObjectConstructInfo
(
object
obj
)
{
Init
(
obj
)
;
}
private
ObjectConstructInfo
(
object
obj
,
Dictionary
<
long
,
ObjectConstructInfo
>
cache
)
{
objCache
=
cache
;
Init
(
obj
)
;
}
private
void
Init
(
object
obj
)
{
bool
_
;
Id
=
idGen
.
GetId
(
obj
,
out
_
)
;
objCache
[
Id
]
=
this
;
Type
=
obj
.
GetType
(
)
;
var
fields
=
Type
.
GetFields
(
BindingFlags
.
NonPublic
|
BindingFlags
.
Public
|
BindingFlags
.
Instance
)
;
Fields
=
Array
.
AsReadOnly
(
fields
)
;
object
[
]
data
=
new
object
[
fields
.
Length
]
;
for
(
int
i
=
0
;
i
<
fields
.
Length
;
i
++
)
{
var
fi
=
fields
[
i
]
;
object
val
=
fi
.
GetValue
(
obj
)
;
Type
t
=
fi
.
FieldType
;
if
(
val
==
null
||
t
.
IsPrimitive
||
(
val
is
MemberInfo
&&
t
.
IsSerializable
)
)
{
data
[
i
]
=
val
;
}
else
if
(
t
.
IsArray
&&
!
t
.
GetElementType
(
)
.
IsPrimitive
)
{
Array
orig
=
(
Array
)
val
;
ObjectConstructInfo
[
]
arr
=
new
ObjectConstructInfo
[
orig
.
Length
]
;
for
(
int
j
=
0
;
j
<
arr
.
Length
;
j
++
)
{
object
aval
=
orig
.
GetValue
(
j
)
;
if
(
aval
!=
null
)
arr
[
j
]
=
new
ObjectConstructInfo
(
aval
,
objCache
)
;
}
data
[
i
]
=
arr
;
}
else
{
ObjectConstructInfo
oci
;
if
(
objCache
.
TryGetValue
(
idGen
.
GetId
(
val
,
out
_
)
,
out
oci
)
)
{
data
[
i
]
=
oci
;
}
else
{
data
[
i
]
=
new
ObjectConstructInfo
(
val
,
objCache
)
;
}
}
}
Data
=
Array
.
AsReadOnly
(
data
)
;
}
public
ObjectConstructInfo
(
Type
type
,
FieldInfo
[
]
fields
,
object
[
]
data
)
{
Type
=
type
;
Fields
=
Array
.
AsReadOnly
(
fields
)
;
Data
=
Array
.
AsReadOnly
(
data
)
;
}
public
object
Construct
(
)
{
Console
.
WriteLine
(
Type
)
;
object
inst
=
FormatterServices
.
GetUninitializedObject
(
Type
)
;
for
(
int
i
=
0
;
i
<
Fields
.
Count
;
i
++
)
{
var
fi
=
Fields
[
i
]
;
object
val
=
Data
[
i
]
;
var
oci
=
val
as
ObjectConstructInfo
;
if
(
oci
!=
null
)
val
=
oci
.
Construct
(
)
;
var
arr
=
val
as
ObjectConstructInfo
[
]
;
if
(
arr
!=
null
)
{
Type
t
=
fi
.
FieldType
.
GetElementType
(
)
;
Array
aval
=
Array
.
CreateInstance
(
t
,
arr
.
Length
)
;
for
(
int
j
=
0
;
j
<
arr
.
Length
;
j
++
)
{
var
aoci
=
arr
[
j
]
;
if
(
aoci
!=
null
)
aval
.
SetValue
(
aoci
.
Construct
(
)
,
j
)
;
}
}
fi
.
SetValue
(
inst
,
val
)
;
}
return
inst
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL