gpt4 book ai didi

c# - 反序列化具有在 AssemblyResolve 上加载的其他程序集中声明的类型字段的对象时出错

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:48 24 4
gpt4 key购买 nike

我有一个应用程序,它在自身内部嵌入(通过 BuildAction:嵌入式资源)引用的程序集(称为 ClassLibrary1)并在 AppDomain.CurrentDomain.AssemblyResolve 事件上加载它。主程序集定义了一个类 Class1:

public class Class1
{
public Class2 MyField { get; set; }
}

它具有在 ClassLibrary1 中定义的 Class2 类型的属性。Class2的定义:

public class Class2
{
public int A { get; set; }
}

在主要方法中,我正在创建一个新的 XmlSerializer(typeof(Class1)):

    static void Main()
{
SubscribeAssemblyResolver();
MainMethod();
}

private static void MainMethod()
{
XmlSerializer xs2 = new XmlSerializer(typeof(Class1));
Class1 cl = new Class1();
}

在执行程序时出现以下错误:

无法生成临时类(结果=1)。错误 CS0012:类型“ClassLibrary1.Class2”在未引用的程序集中定义。您必须添加对程序集“ClassLibrary1,Version=1.0.0.0,Culture=neutral,PublicKeyToken=c06f123f2868e8c8”的引用。错误 CS0266:无法将类型“对象”隐式转换为“ClassLibrary1.Class2”。存在显式转换(是否缺少强制转换?)

有什么想法吗?

其余代码:

    private static void SubscribeAssemblyResolver()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}

static Dictionary<String, Assembly> _assemblies = new Dictionary<String, Assembly>(StringComparer.OrdinalIgnoreCase);

static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
return ResolveAssembly(args.Name);
}

private static Assembly ResolveAssembly(string argsName)
{
Assembly dll;
var name = "WindowsFormsApplication1.Libs." + new AssemblyName(argsName).Name + ".dll";
if (!_assemblies.TryGetValue(name, out dll))
{
Assembly res = typeof(Program).Assembly;
using (var input = res.GetManifestResourceStream(name))
{
if (input == null)
{
//TODO: log
return null;
}
Byte[] assemblyData = new Byte[input.Length];
input.Read(assemblyData, 0, assemblyData.Length);
if (null == (dll = Assembly.Load(assemblyData)))
{
//TODO: log
return null;
}
//TODO: log
_assemblies[name] = dll;
return dll;
}
}
return dll;
}

更新:创建了一个 BUG在 Microsoft Connect 站点上。您还可以从那里下载一个示例 visual stuido 2010 解决方案(只需展开详细信息字段组)以重现它。

最佳答案

我通过将程序集保存在临时文件夹中解决了类似的问题

    public static byte[] ReadFully(Stream input)
{
var buffer = new byte[16 * 1024];
using (var ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}

public App()
{
AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
var assemblyName = new AssemblyName(args.Name);

if (assemblyName.Name != "Omikad.Core")
return null;

var resourceName = "Terem." + assemblyName.Name + ".dll";

using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
if (stream == null)
return null;

var assemblyData = ReadFully(stream);
var tmp = Path.Combine(Path.GetTempPath(), "Omikad.Core.dll");
File.WriteAllBytes(tmp, assemblyData);
return Assembly.LoadFrom(tmp);
}
};
}

关于c# - 反序列化具有在 AssemblyResolve 上加载的其他程序集中声明的类型字段的对象时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6055858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com