-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed serialization behavior for dynamic type resolving (now the se…
…rializer uses type aliases instead of real type names)
- Loading branch information
1 parent
eef50e9
commit f85e92a
Showing
13 changed files
with
115 additions
and
0 deletions.
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
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
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
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
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
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
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
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
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
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,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MessageCommunicator.TestGui | ||
{ | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] | ||
public class TypeAliasAttribute : Attribute | ||
{ | ||
public string AliasName { get; } | ||
|
||
public TypeAliasAttribute(string aliasName) | ||
{ | ||
this.AliasName = aliasName; | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...geCommunicator.TestGui/_Util/_Serialization/DynamicMappingWithAliasSerializationBinder.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,56 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Text; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
namespace MessageCommunicator.TestGui | ||
{ | ||
public class DynamicMappingWithAliasSerializationBinder : DefaultSerializationBinder | ||
{ | ||
private const string ASSEMBLY_ALIAS = "__alias"; | ||
|
||
private ResolveTypeByAliasDelegate _typeResolver; | ||
|
||
public DynamicMappingWithAliasSerializationBinder(ResolveTypeByAliasDelegate typeResolver) | ||
{ | ||
_typeResolver = typeResolver; | ||
} | ||
|
||
public override void BindToName(Type serializedType, out string assemblyName, out string typeName) | ||
{ | ||
var aliasAttrib = serializedType.GetCustomAttribute<TypeAliasAttribute>(); | ||
if (aliasAttrib == null) | ||
{ | ||
throw new JsonSerializationException( | ||
$"Unable to serialize type {serializedType.FullName} because dynamic mapping is only supported for types with {nameof(TypeAliasAttribute)}!"); | ||
} | ||
|
||
if (serializedType.IsGenericType) | ||
{ | ||
throw new JsonSerializationException( | ||
$"Unable to serialize type {serializedType.FullName} because dynamic mapping of generic types is not supported!"); | ||
} | ||
|
||
assemblyName = ASSEMBLY_ALIAS; | ||
typeName = aliasAttrib.AliasName; | ||
} | ||
|
||
public override Type BindToType(string? assemblyName, string typeName) | ||
{ | ||
if (assemblyName != ASSEMBLY_ALIAS) | ||
{ | ||
throw new JsonSerializationException($"Unable to load type {typeName} from assembly {assemblyName}: Dynamic resolving only available for alias names!"); | ||
} | ||
|
||
var resolvedType = _typeResolver(typeName); | ||
if (resolvedType == null) | ||
{ | ||
throw new JsonSerializationException($"Unable to load type {typeName} from assembly {assemblyName}!"); | ||
} | ||
|
||
return resolvedType; | ||
} | ||
} | ||
} |
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
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,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MessageCommunicator.TestGui | ||
{ | ||
public delegate Type? ResolveTypeByAliasDelegate(string aliasName); | ||
} |