i有一个.NET标准2.0应用程序,该应用程序在.net45 dll中引用某些合同。我以这种方式这样做的是,一旦这些合同对象序列化,它们就会使用.NET45组装类型。使用.NET45库(这是最终目标)现在给出错误:
json'System.collections.generic.dictionary`2 [[System.String,System.private.corelib],[System.Striping,System.private.corelib],System.private.Corelib'
显然是因为它试图从标准组件类型中解析字符串类型,而不是从Mscorlib中解析。有什么方法可以实现我的尝试?
有许多不同的方法可以解决这个问题。
对于大型代码碱体,无法轻松转换为自定义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;
}