[英].Net 45 serialisation from .Net Standard 2.0 dll
I have a .Net standard 2.0 app that is referencing some contracts in a .Net45 dll.我有一个 .Net 标准 2.0 应用程序,它在 .Net45 dll 中引用了一些合同。 I was doing it this way under the impression that once these contract objects get serialised they will be done so using the .Net45 assembly types.我这样做的印象是,一旦这些合同对象被序列化,它们将使用 .Net45 程序集类型来完成。 Deserilising these using a .Net45 library (which is the end goal) is now giving the error:使用 .Net45 库(这是最终目标)对这些进行反序列化现在给出错误:
Error resolving type specified in JSON 'System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[System.String, System.Private.CoreLib]], System.Private.CoreLib'解析 JSON 'System.Collections.Generic.Dictionary`2[[System.String, System.Private.CoreLib],[System.String, System.Private.CoreLib]], System.Private.CoreLib' 中指定的类型时出错
Which is obviously because it is trying to resolve the string type from the Standard assembly type, not from mscorlib.这显然是因为它试图从标准程序集类型而不是 mscorlib 解析字符串类型。 Is there any way of achieving what I am attempting?有什么方法可以实现我正在尝试的目标吗?
There are many different ways to can tackle this.有许多不同的方法可以解决这个问题。
For large code-base that cannot be easily converted to a custom ISerializationBinder I have implement a redirect (not pretty but it works)对于无法轻松转换为自定义 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.