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
123 lines (111 loc) · 3.93 KB
/
MaybeMemberInfo.cs
File metadata and controls
123 lines (111 loc) · 3.93 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
119
120
121
122
123
using System;
using System.Reflection;
using System.Runtime.Serialization;
namespace Python.Runtime
{
[Serializable]
internal struct MaybeMemberInfo<T> : ISerializable where T : MemberInfo
{
// .ToString() of the serialized object
const string SerializationDescription = "d";
// The ReflectedType of the object
const string SerializationType = "t";
const string SerializationMemberName = "n";
MemberInfo? info;
[NonSerialized]
Exception? deserializationException;
public string DeletedMessage
{
get
{
return $"The .NET {typeof(T).Name} {Description} no longer exists. Cause: " + deserializationException?.Message ;
}
}
public T Value
{
get
{
if (info == null)
{
throw new SerializationException(DeletedMessage, innerException: deserializationException);
}
return (T)info;
}
}
public string Description { get; }
public bool Valid => info != null;
public override string ToString()
{
return (info != null ? info.ToString() : $"missing: {Description}");
}
public MaybeMemberInfo(T fi)
{
info = fi;
Description = info.ToString();
if (info.DeclaringType is not null)
Description += " of " + info.DeclaringType;
deserializationException = null;
}
internal MaybeMemberInfo(SerializationInfo serializationInfo, StreamingContext context)
{
Description = serializationInfo.GetString(SerializationDescription);
info = null;
deserializationException = null;
try
{
var tp = Type.GetType(serializationInfo.GetString(SerializationType));
if (tp != null)
{
var memberName = serializationInfo.GetString(SerializationMemberName);
MemberInfo? mi = Get(tp, memberName, ClassManager.BindingFlags);
if (mi != null && ShouldBindMember(mi))
{
info = mi;
}
}
}
catch (Exception e)
{
deserializationException = e;
}
}
static MemberInfo? Get(Type type, string name, BindingFlags flags)
{
if (typeof(T) == typeof(FieldInfo))
return type.GetField(name, flags);
if (typeof(T) == typeof(PropertyInfo))
return type.GetProperty(name, flags);
throw new NotImplementedException(typeof(T).Name);
}
// 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(SerializationDescription, Description);
if (info is not null)
{
serializationInfo.AddValue(SerializationMemberName, info.Name);
serializationInfo.AddValue(SerializationType, info.ReflectedType.AssemblyQualifiedName);
}
}
}
}