| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,8 +1,6 @@ | |||
| 1 | 1 | using System; | |
| 2 | 2 | using System.Reflection; | |
| 3 | 3 | using System.Runtime.Serialization; | |
| 4 | - using System.Runtime.Serialization.Formatters.Binary; | ||
| 5 | - using System.IO; | ||
| 6 | 4 | ||
| 7 | 5 | namespace Python.Runtime | |
| 8 | 6 | { | |
@@ -12,21 +10,20 @@ internal struct MaybeMemberInfo<T> : ISerializable where T : MemberInfo | |||
| 12 | 10 | public static implicit operator MaybeMemberInfo<T>(T ob) => new MaybeMemberInfo<T>(ob); | |
| 13 | 11 | ||
| 14 | 12 | // .ToString() of the serialized object | |
| 15 | - const string SerializationName = "s"; | ||
| 13 | + const string SerializationDescription = "d"; | ||
| 16 | 14 | // The ReflectedType of the object | |
| 17 | 15 | const string SerializationType = "t"; | |
| 18 | - const string SerializationFieldName = "f"; | ||
| 19 | - string name; | ||
| 20 | - MemberInfo info; | ||
| 16 | + const string SerializationMemberName = "n"; | ||
| 17 | + MemberInfo? info; | ||
| 21 | 18 | ||
| 22 | 19 | [NonSerialized] | |
| 23 | - Exception deserializationException; | ||
| 20 | + Exception? deserializationException; | ||
| 24 | 21 | ||
| 25 | 22 | public string DeletedMessage | |
| 26 | 23 | { | |
| 27 | 24 | get | |
| 28 | 25 | { | |
| 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 ; | ||
| 30 | 27 | } | |
| 31 | 28 | } | |
| 32 | 29 | ||
@@ -42,34 +39,35 @@ public T Value | |||
| 42 | 39 | } | |
| 43 | 40 | } | |
| 44 | 41 | ||
| 45 | - public string Name => name; | ||
| 42 | + public string Description { get; } | ||
| 46 | 43 | public bool Valid => info != null; | |
| 47 | 44 | ||
| 48 | 45 | public override string ToString() | |
| 49 | 46 | { | |
| 50 | - return (info != null ? info.ToString() : $"missing type: {name}"); | ||
| 47 | + return (info != null ? info.ToString() : $"missing: {Description}"); | ||
| 51 | 48 | } | |
| 52 | 49 | ||
| 53 | 50 | public MaybeMemberInfo(T fi) | |
| 54 | 51 | { | |
| 55 | 52 | info = fi; | |
| 56 | - name = info?.ToString(); | ||
| 53 | + Description = info?.ToString(); | ||
| 54 | + if (info?.DeclaringType is not null) | ||
| 55 | + Description += " of " + info.DeclaringType; | ||
| 57 | 56 | deserializationException = null; | |
| 58 | 57 | } | |
| 59 | 58 | ||
| 60 | 59 | internal MaybeMemberInfo(SerializationInfo serializationInfo, StreamingContext context) | |
| 61 | 60 | { | |
| 62 | - // Assumption: name is always stored in "s" | ||
| 63 | - name = serializationInfo.GetString(SerializationName); | ||
| 61 | + Description = serializationInfo.GetString(SerializationDescription); | ||
| 64 | 62 | info = null; | |
| 65 | 63 | deserializationException = null; | |
| 66 | 64 | try | |
| 67 | 65 | { | |
| 68 | 66 | var tp = Type.GetType(serializationInfo.GetString(SerializationType)); | |
| 69 | 67 | if (tp != null) | |
| 70 | 68 | { | |
| 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); | ||
| 73 | 71 | if (mi != null && ShouldBindMember(mi)) | |
| 74 | 72 | { | |
| 75 | 73 | info = mi; | |
@@ -82,6 +80,15 @@ internal MaybeMemberInfo(SerializationInfo serializationInfo, StreamingContext c | |||
| 82 | 80 | } | |
| 83 | 81 | } | |
| 84 | 82 | ||
| 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 | + | ||
| 85 | 92 | // This is complicated because we bind fields | |
| 86 | 93 | // based on the visibility of the field, properties | |
| 87 | 94 | // based on it's setter/getter (which is a method | |
@@ -107,10 +114,10 @@ static bool ShouldBindMember(MemberInfo mi) | |||
| 107 | 114 | ||
| 108 | 115 | public void GetObjectData(SerializationInfo serializationInfo, StreamingContext context) | |
| 109 | 116 | { | |
| 110 | - serializationInfo.AddValue(SerializationName, name); | ||
| 111 | - if (Valid) | ||
| 117 | + serializationInfo.AddValue(SerializationDescription, Description); | ||
| 118 | + if (info is not null) | ||
| 112 | 119 | { | |
| 113 | - serializationInfo.AddValue(SerializationFieldName, info.Name); | ||
| 120 | + serializationInfo.AddValue(SerializationMemberName, info.Name); | ||
| 114 | 121 | serializationInfo.AddValue(SerializationType, info.ReflectedType.AssemblyQualifiedName); | |
| 115 | 122 | } | |
| 116 | 123 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments