|
3 | 3 | using System.Runtime.Serialization;
|
4 | 4 | using System.Runtime.Serialization.Formatters.Binary;
|
5 | 5 | using System.IO;
|
| 6 | +using System.Collections.Generic; |
6 | 7 |
|
7 |
| -namespace Python.Runtime |
| 8 | +namespace Python.Runtime; |
| 9 | + |
| 10 | +[Serializable] |
| 11 | +internal struct MaybeType : ISerializable |
8 | 12 | {
|
9 |
| - [Serializable] |
10 |
| - internal struct MaybeType : ISerializable |
11 |
| - { |
12 |
| - public static implicit operator MaybeType (Type ob) => new(ob); |
| 13 | + public static implicit operator MaybeType (Type ob) => new(ob); |
13 | 14 |
|
14 |
| - // The AssemblyQualifiedName of the serialized Type |
15 |
| - const string SerializationName = "n"; |
16 |
| - readonly string name; |
17 |
| - readonly Type type; |
| 15 | + // The AssemblyQualifiedName of the serialized Type |
| 16 | + const string SerializationName = "n"; |
| 17 | + readonly string name; |
| 18 | + readonly Type type; |
18 | 19 |
|
19 |
| - public string DeletedMessage |
| 20 | + public string DeletedMessage |
| 21 | + { |
| 22 | + get |
20 | 23 | {
|
21 |
| - get |
22 |
| - { |
23 |
| - return $"The .NET Type {name} no longer exists"; |
24 |
| - } |
| 24 | + return $"The .NET Type {name} no longer exists"; |
25 | 25 | }
|
| 26 | + } |
26 | 27 |
|
27 |
| - public Type Value |
| 28 | + public Type Value |
| 29 | + { |
| 30 | + get |
28 | 31 | {
|
29 |
| - get |
| 32 | + if (type == null) |
30 | 33 | {
|
31 |
| - if (type == null) |
32 |
| - { |
33 |
| - throw new SerializationException(DeletedMessage); |
34 |
| - } |
35 |
| - return type; |
| 34 | + throw new SerializationException(DeletedMessage); |
36 | 35 | }
|
| 36 | + return type; |
37 | 37 | }
|
| 38 | + } |
38 | 39 |
|
39 |
| - public string Name => name; |
40 |
| - public bool Valid => type != null; |
| 40 | + public string Name => name; |
| 41 | + public bool Valid => type != null; |
41 | 42 |
|
42 |
| - public override string ToString() |
43 |
| - { |
44 |
| - return (type != null ? type.ToString() : $"missing type: {name}"); |
45 |
| - } |
| 43 | + public override string ToString() |
| 44 | + { |
| 45 | + return (type != null ? type.ToString() : $"missing type: {name}"); |
| 46 | + } |
46 | 47 |
|
47 |
| - public MaybeType(Type tp) |
48 |
| - { |
49 |
| - type = tp; |
50 |
| - name = tp.AssemblyQualifiedName; |
51 |
| - } |
| 48 | + public MaybeType(Type tp) |
| 49 | + { |
| 50 | + type = tp; |
| 51 | + name = tp.AssemblyQualifiedName; |
| 52 | + } |
52 | 53 |
|
53 |
| - private MaybeType(SerializationInfo serializationInfo, StreamingContext context) |
54 |
| - { |
55 |
| - name = (string)serializationInfo.GetValue(SerializationName, typeof(string)); |
56 |
| - type = Type.GetType(name, throwOnError:false); |
57 |
| - } |
| 54 | + private MaybeType(SerializationInfo serializationInfo, StreamingContext context) |
| 55 | + { |
| 56 | + name = (string)serializationInfo.GetValue(SerializationName, typeof(string)); |
| 57 | + type = Type.GetType(name, throwOnError:false); |
| 58 | + } |
58 | 59 |
|
59 |
| - public void GetObjectData(SerializationInfo serializationInfo, StreamingContext context) |
60 |
| - { |
61 |
| - serializationInfo.AddValue(SerializationName, name); |
62 |
| - } |
| 60 | + public void GetObjectData(SerializationInfo serializationInfo, StreamingContext context) |
| 61 | + { |
| 62 | + serializationInfo.AddValue(SerializationName, name); |
63 | 63 | }
|
64 | 64 | }
|
| 65 | + |
| 66 | +[Serializable] |
| 67 | +internal class MaybeTypeComparer : IEqualityComparer<MaybeType> |
| 68 | +{ |
| 69 | + public bool Equals (MaybeType lhs, MaybeType rhs) => |
| 70 | + lhs.Name == rhs.Name; |
| 71 | + |
| 72 | + public int GetHashCode(MaybeType t) => t.Name.GetHashCode(); |
| 73 | +} |
0 commit comments