-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Serializing and deserializing in different frameworks #1378
Comments
Serializing between different frameworks with the type name is unfortunately a very difficult problem. Just a quick look at https://apisof.net/catalog/System.EventArgs shows that I'm really not sure what solution there can be for this. Perhaps a library could be created to map the various system type names to it's proper type for the runtime framework and the |
Implement your own ISerializationBinder. With it you can customize the generatation and handling of the type name. |
For large code-base that vannot be easily converted to a custom ISerializationBinder RedirectAssembly("System.Private.CoreLib", "mscorlib");
public static void RedirectAssembly(string fromAssemblyShotName, string replacmentAssemblyShortName)
{
Console.WriteLine($"Adding custom resolver redirect rule form:{fromAssemblyShotName}, to:{replacmentAssemblyShortName}");
ResolveEventHandler handler = null;
handler = (sender, args) =>
{
// Use latest strong name & version when trying to load SDK assemblies
var requestedAssembly = new AssemblyName(args.Name);
Console.WriteLine($"RedirectAssembly > requesting:{requestedAssembly}; replacment from:{fromAssemblyShotName}, to:{replacmentAssemblyShortName}");
if (requestedAssembly.Name == fromAssemblyShotName)
{
try
{
Console.WriteLine($"Redirecting Assembly {fromAssemblyShotName} to: {replacmentAssemblyShortName}");
var replacmentAssembly = Assembly.Load(replacmentAssemblyShortName);
return replacmentAssembly;
}
catch (Exception e)
{
Console.WriteLine($"ERROR while trying to provide replacement Assembly {fromAssemblyShotName} to: {replacmentAssemblyShortName}");
Console.WriteLine(e);
return null;
}
}
Console.WriteLine($"Framework faild to find {requestedAssembly}, trying to provide replacment from:{fromAssemblyShotName}, to:{replacmentAssemblyShortName}");
return null;
};
AppDomain.CurrentDomain.AssemblyResolve += handler;
} |
To my knowledge, every serializer in the .NET BCL automatically adjusts type names to account for the As for anyone doing simple string replacement or the assembly resolve redirection above, it's possible for types in an assembly to be forwarded to multiple separate assemblies (and this does, in fact, happen with some types that have been reorganized in .NET Standard), so while it might work well enough for limited use cases, in some conditions it will not. |
One can extend internal sealed class DotNetCompatibleSerializationBinder : DefaultSerializationBinder
{
private const string CoreLibAssembly = "System.Private.CoreLib";
private const string MscorlibAssembly = "mscorlib";
public override Type BindToType(string assemblyName, string typeName)
{
if (assemblyName == CoreLibAssembly)
{
assemblyName = MscorlibAssembly;
typeName = typeName.Replace(CoreLibAssembly, MscorlibAssembly);
}
return base.BindToType(assemblyName, typeName);
}
} and then: var settings = new JsonSerializerSettings()
{
SerializationBinder = new DotNetCompatibleSerializationBinder()
}; |
These hacks that remap The correct solution is to honor Also note that this isn't just a .NETCore problem. Similar moves happened in .NETFramework https://referencesource.microsoft.com/#mscorlib/system/runtime/compilerservices/TypeForwardedFromAttribute.cs,a0ffa0ad2414acf4,references |
I'm not sure, if this is a bug or just not implemented.
Having a class with a property of type "Type", once in a .Net framework and once in a UWP framework.
The type serializes with the AssemblyQualifiedName property of the type.
Thus serializing in the .Net framework rusults in something like
"System.EventArgs, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
If I deserialize this in an UWP application with JsonConver.DeserializeObject a propper Type object is created. Serializing this one in UWP, the following string is produced
"System.EventArgs, System.Private.CoreLib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = 7cec85d7bea7798e"
Trying to deserialize this back in a .Net framework application, I get the error
"Error converting value "System.EventArgs, System.Private.CoreLib, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = 7cec85d7bea7798e" to type 'System.Type'."
The message is clear to me, but then I wouldn't expect why it works in the other direction. Is this due to tha fact, the UWP version is newer and can handle this convertion or is it a bug?
The text was updated successfully, but these errors were encountered: