11using System ;
22using System . Reflection ;
33using System . Runtime . Serialization ;
4- using System . Runtime . Serialization . Formatters . Binary ;
5- using System . IO ;
64
75namespace Python . Runtime
86{
@@ -12,21 +10,20 @@ internal struct MaybeMemberInfo<T> : ISerializable where T : MemberInfo
1210 public static implicit operator MaybeMemberInfo < T > ( T ob ) => new MaybeMemberInfo < T > ( ob ) ;
1311
1412 // .ToString() of the serialized object
15- const string SerializationName = "s " ;
13+ const string SerializationDescription = "d " ;
1614 // The ReflectedType of the object
1715 const string SerializationType = "t" ;
18- const string SerializationFieldName = "f" ;
19- string name ;
20- MemberInfo info ;
16+ const string SerializationMemberName = "n" ;
17+ MemberInfo ? info ;
2118
2219 [ NonSerialized ]
23- Exception deserializationException ;
20+ Exception ? deserializationException ;
2421
2522 public string DeletedMessage
2623 {
2724 get
2825 {
29- return $ "The .NET { typeof ( T ) } { name } no longer exists. Cause: " + deserializationException ? . Message ;
26+ return $ "The .NET { typeof ( T ) . Name } { Description } no longer exists. Cause: " + deserializationException ? . Message ;
3027 }
3128 }
3229
@@ -42,34 +39,35 @@ public T Value
4239 }
4340 }
4441
45- public string Name => name ;
42+ public string Description { get ; }
4643 public bool Valid => info != null ;
4744
4845 public override string ToString ( )
4946 {
50- return ( info != null ? info . ToString ( ) : $ "missing type : { name } ") ;
47+ return ( info != null ? info . ToString ( ) : $ "missing: { Description } ") ;
5148 }
5249
5350 public MaybeMemberInfo ( T fi )
5451 {
5552 info = fi ;
56- name = info ? . ToString ( ) ;
53+ Description = info ? . ToString ( ) ;
54+ if ( info ? . DeclaringType is not null )
55+ Description += " of " + info . DeclaringType ;
5756 deserializationException = null ;
5857 }
5958
6059 internal MaybeMemberInfo ( SerializationInfo serializationInfo , StreamingContext context )
6160 {
62- // Assumption: name is always stored in "s"
63- name = serializationInfo . GetString ( SerializationName ) ;
61+ Description = serializationInfo . GetString ( SerializationDescription ) ;
6462 info = null ;
6563 deserializationException = null ;
6664 try
6765 {
6866 var tp = Type . GetType ( serializationInfo . GetString ( SerializationType ) ) ;
6967 if ( tp != null )
7068 {
71- var field_name = serializationInfo . GetString ( SerializationFieldName ) ;
72- MemberInfo mi = tp . GetField ( field_name , ClassManager . BindingFlags ) ;
69+ var memberName = serializationInfo . GetString ( SerializationMemberName ) ;
70+ MemberInfo ? mi = Get ( tp , memberName , ClassManager . BindingFlags ) ;
7371 if ( mi != null && ShouldBindMember ( mi ) )
7472 {
7573 info = mi ;
@@ -82,6 +80,15 @@ internal MaybeMemberInfo(SerializationInfo serializationInfo, StreamingContext c
8280 }
8381 }
8482
83+ static MemberInfo ? Get ( Type type , string name , BindingFlags flags )
84+ {
85+ if ( typeof ( T ) == typeof ( FieldInfo ) )
86+ return type . GetField ( name , flags ) ;
87+ if ( typeof ( T ) == typeof ( PropertyInfo ) )
88+ return type . GetProperty ( name , flags ) ;
89+ throw new NotImplementedException ( typeof ( T ) . Name ) ;
90+ }
91+
8592 // This is complicated because we bind fields
8693 // based on the visibility of the field, properties
8794 // based on it's setter/getter (which is a method
@@ -107,10 +114,10 @@ static bool ShouldBindMember(MemberInfo mi)
107114
108115 public void GetObjectData ( SerializationInfo serializationInfo , StreamingContext context )
109116 {
110- serializationInfo . AddValue ( SerializationName , name ) ;
111- if ( Valid )
117+ serializationInfo . AddValue ( SerializationDescription , Description ) ;
118+ if ( info is not null )
112119 {
113- serializationInfo . AddValue ( SerializationFieldName , info . Name ) ;
120+ serializationInfo . AddValue ( SerializationMemberName , info . Name ) ;
114121 serializationInfo . AddValue ( SerializationType , info . ReflectedType . AssemblyQualifiedName ) ;
115122 }
116123 }
0 commit comments