-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try a custom SerializationBinder so that moving namespaces doesn't co…
…mpletely break filters
- Loading branch information
Showing
2 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
Shoko.Server/Databases/NHIbernate/SimpleNameSerializationBinder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Linq; | ||
using Newtonsoft.Json.Serialization; | ||
using NLog; | ||
|
||
namespace Shoko.Server.Databases.NHIbernate; | ||
|
||
public class SimpleNameSerializationBinder : DefaultSerializationBinder | ||
{ | ||
private readonly Logger _logger = LogManager.GetCurrentClassLogger(); | ||
private readonly Type _baseType; | ||
|
||
public SimpleNameSerializationBinder(Type baseType = null) | ||
{ | ||
_baseType = baseType; | ||
} | ||
|
||
public override void BindToName( | ||
Type serializedType, out string assemblyName, out string typeName) | ||
{ | ||
assemblyName = null; | ||
typeName = serializedType.Name; | ||
} | ||
|
||
public override Type BindToType(string assemblyName, string typeName) | ||
{ | ||
var name = typeName.Split('.').LastOrDefault(); | ||
var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(a => a.GetTypes()) | ||
.Where(a => a.Name.Equals(name) && (_baseType == null || _baseType.IsAssignableFrom(a))).ToArray(); | ||
if (types.Length > 1) _logger.Warn($"SimpleNameSerializationBinder found multiple types that match {name}"); | ||
return types.FirstOrDefault(); | ||
} | ||
} |