This repository was archived by the owner on Jul 22, 2023. It is now read-only.
forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaybeMemberInfo.cs
More file actions
118 lines (106 loc) · 3.64 KB
/
MaybeMemberInfo.cs
File metadata and controls
118 lines (106 loc) · 3.64 KB
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
using System;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace Python.Runtime
{
[Serializable]
internal struct MaybeMemberInfo<T> : ISerializable where T : MemberInfo
{
public static implicit operator MaybeMemberInfo<T>(T ob) => new MaybeMemberInfo<T>(ob);
// .ToString() of the serialized object
const string SerializationName = "s";
// The ReflectedType of the object
const string SerializationType = "t";
const string SerializationFieldName = "f";
string name;
MemberInfo info;
[NonSerialized]
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 string Name => name;
public bool Valid => info != null;
public override string ToString()
{
return (info != null ? info.ToString() : $"missing type: {name}");
}
public MaybeMemberInfo(T fi)
{
info = fi;
name = info?.ToString();
deserializationException = null;
}
internal MaybeMemberInfo(SerializationInfo serializationInfo, StreamingContext context)
{
// Assumption: name is always stored in "s"
name = serializationInfo.GetString(SerializationName);
info = null;
deserializationException = null;
try
{
var tp = Type.GetType(serializationInfo.GetString(SerializationType));
if (tp != null)
{
var field_name = serializationInfo.GetString(SerializationFieldName);
MemberInfo mi = tp.GetField(field_name, ClassManager.BindingFlags);
if (mi != null && ShouldBindMember(mi))
{
info = mi;
}
}
}
catch (Exception e)
{
deserializationException = e;
}
}
// This is complicated because we bind fields
// based on the visibility of the field, properties
// based on it's setter/getter (which is a method
// info) visibility and events based on their
// AddMethod visibility.
static bool ShouldBindMember(MemberInfo mi)
{
if (mi is PropertyInfo pi)
{
return ClassManager.ShouldBindProperty(pi);
}
else if (mi is FieldInfo fi)
{
return ClassManager.ShouldBindField(fi);
}
else if (mi is EventInfo ei)
{
return ClassManager.ShouldBindEvent(ei);
}
return false;
}
public void GetObjectData(SerializationInfo serializationInfo, StreamingContext context)
{
serializationInfo.AddValue(SerializationName, name);
if (Valid)
{
serializationInfo.AddValue(SerializationFieldName, info.Name);
serializationInfo.AddValue(SerializationType, info.ReflectedType.AssemblyQualifiedName);
}
}
}
}