Add a workaround for unbinding script members on Mono
This commit is contained in:
@@ -24,6 +24,10 @@ namespace OpenRA.Scripting
|
|||||||
protected readonly ScriptContext Context;
|
protected readonly ScriptContext Context;
|
||||||
readonly Dictionary<string, ScriptMemberWrapper> members = new Dictionary<string, ScriptMemberWrapper>();
|
readonly Dictionary<string, ScriptMemberWrapper> members = new Dictionary<string, ScriptMemberWrapper>();
|
||||||
|
|
||||||
|
#if !NET5_0_OR_GREATER
|
||||||
|
readonly List<string> membersToRemove = new List<string>();
|
||||||
|
#endif
|
||||||
|
|
||||||
public ScriptObjectWrapper(ScriptContext context)
|
public ScriptObjectWrapper(ScriptContext context)
|
||||||
{
|
{
|
||||||
Context = context;
|
Context = context;
|
||||||
@@ -63,9 +67,22 @@ namespace OpenRA.Scripting
|
|||||||
|
|
||||||
protected void Unbind(Type targetType)
|
protected void Unbind(Type targetType)
|
||||||
{
|
{
|
||||||
|
#if NET5_0_OR_GREATER
|
||||||
|
// NOTE: In newer versions of .NET modifying the collection by calling Remove while iterating over it is valid
|
||||||
foreach (var m in members)
|
foreach (var m in members)
|
||||||
if (targetType == m.Value.Target.GetType())
|
if (targetType == m.Value.Target.GetType())
|
||||||
members.Remove(m.Key);
|
members.Remove(m.Key);
|
||||||
|
#else
|
||||||
|
// PERF: Re-use instead of allocating a new list on each unbind
|
||||||
|
membersToRemove.Clear();
|
||||||
|
|
||||||
|
foreach (var m in members)
|
||||||
|
if (targetType == m.Value.Target.GetType())
|
||||||
|
membersToRemove.Add(m.Key);
|
||||||
|
|
||||||
|
foreach (var m in membersToRemove)
|
||||||
|
members.Remove(m);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ContainsKey(string key) { return members.ContainsKey(key); }
|
public bool ContainsKey(string key) { return members.ContainsKey(key); }
|
||||||
|
|||||||
Reference in New Issue
Block a user