Skip to content

Commit bccee2c

Browse files
committed
Explicitly compare MaybeType objects by name
1 parent 5c50b20 commit bccee2c

File tree

2 files changed

+53
-43
lines changed

2 files changed

+53
-43
lines changed

src/runtime/ClassManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Diagnostics;
44
using System.Linq;
55
using System.Reflection;
6-
using System.Runtime.InteropServices;
76
using System.Security;
87

98
using Python.Runtime.StateSerialization;
@@ -33,7 +32,9 @@ internal class ClassManager
3332
BindingFlags.Public |
3433
BindingFlags.NonPublic;
3534

36-
internal static Dictionary<MaybeType, ReflectedClrType> cache = new(capacity: 128);
35+
internal static Dictionary<MaybeType, ReflectedClrType> cache = new(
36+
capacity: 128, comparer: new MaybeTypeComparer()
37+
);
3738
private static readonly Type dtype;
3839

3940
private ClassManager()

src/runtime/StateSerialization/MaybeType.cs

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,71 @@
33
using System.Runtime.Serialization;
44
using System.Runtime.Serialization.Formatters.Binary;
55
using System.IO;
6+
using System.Collections.Generic;
67

7-
namespace Python.Runtime
8+
namespace Python.Runtime;
9+
10+
[Serializable]
11+
internal struct MaybeType : ISerializable
812
{
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);
1314

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;
1819

19-
public string DeletedMessage
20+
public string DeletedMessage
21+
{
22+
get
2023
{
21-
get
22-
{
23-
return $"The .NET Type {name} no longer exists";
24-
}
24+
return $"The .NET Type {name} no longer exists";
2525
}
26+
}
2627

27-
public Type Value
28+
public Type Value
29+
{
30+
get
2831
{
29-
get
32+
if (type == null)
3033
{
31-
if (type == null)
32-
{
33-
throw new SerializationException(DeletedMessage);
34-
}
35-
return type;
34+
throw new SerializationException(DeletedMessage);
3635
}
36+
return type;
3737
}
38+
}
3839

39-
public string Name => name;
40-
public bool Valid => type != null;
40+
public string Name => name;
41+
public bool Valid => type != null;
4142

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+
}
4647

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+
}
5253

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+
}
5859

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);
6363
}
6464
}
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy