Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Explicitly compare MaybeType objects by name#2270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
filmor wants to merge1 commit intomaster
base:master
Choose a base branch
Loading
fromcompare-maybetype-by-name
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletionssrc/runtime/ClassManager.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,6 @@
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;

using Python.Runtime.StateSerialization;
Expand DownExpand Up@@ -33,7 +32,9 @@ internal class ClassManager
BindingFlags.Public |
BindingFlags.NonPublic;

internal static Dictionary<MaybeType, ReflectedClrType> cache = new(capacity: 128);
internal static Dictionary<MaybeType, ReflectedClrType> cache = new(
capacity: 128, comparer: new MaybeTypeComparer()
);
private static readonly Type dtype;

private ClassManager()
Expand Down
91 changes: 50 additions & 41 deletionssrc/runtime/StateSerialization/MaybeType.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,62 +3,71 @@
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections.Generic;

namespace Python.Runtime
namespace Python.Runtime;

[Serializable]
internal struct MaybeType : ISerializable
{
[Serializable]
internal struct MaybeType : ISerializable
{
public static implicit operator MaybeType (Type ob) => new(ob);
public static implicit operator MaybeType (Type ob) => new(ob);

// The AssemblyQualifiedName of the serialized Type
const string SerializationName = "n";
readonly string name;
readonly Type type;
// The AssemblyQualifiedName of the serialized Type
const string SerializationName = "n";
readonly string name;
readonly Type type;

public string DeletedMessage
public string DeletedMessage
{
get
{
get
{
return $"The .NET Type {name} no longer exists";
}
return $"The .NET Type {name} no longer exists";
}
}

public Type Value
public Type Value
{
get
{
get
if (type == null)
{
if (type == null)
{
throw new SerializationException(DeletedMessage);
}
return type;
throw new SerializationException(DeletedMessage);
}
return type;
}
}

public string Name => name;
public bool Valid => type != null;
public string Name => name;
public bool Valid => type != null;

public override string ToString()
{
return (type != null ? type.ToString() : $"missing type: {name}");
}
public override string ToString()
{
return (type != null ? type.ToString() : $"missing type: {name}");
}

public MaybeType(Type tp)
{
type = tp;
name = tp.AssemblyQualifiedName;
}
public MaybeType(Type tp)
{
type = tp;
name = tp.AssemblyQualifiedName;
}

private MaybeType(SerializationInfo serializationInfo, StreamingContext context)
{
name = (string)serializationInfo.GetValue(SerializationName, typeof(string));
type = Type.GetType(name, throwOnError:false);
}
private MaybeType(SerializationInfo serializationInfo, StreamingContext context)
{
name = (string)serializationInfo.GetValue(SerializationName, typeof(string));
type = Type.GetType(name, throwOnError:false);
}

public void GetObjectData(SerializationInfo serializationInfo, StreamingContext context)
{
serializationInfo.AddValue(SerializationName, name);
}
public void GetObjectData(SerializationInfo serializationInfo, StreamingContext context)
{
serializationInfo.AddValue(SerializationName, name);
}
}

[Serializable]
internal class MaybeTypeComparer : IEqualityComparer<MaybeType>
{
public bool Equals (MaybeType lhs, MaybeType rhs) =>
lhs.Name == rhs.Name;

public int GetHashCode(MaybeType t) => (t.Name ?? "").GetHashCode();
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp