0

System.Data.SQLite.DLL 是一个混合代码 DLL。它包含 C 和 C#。我知道如何将其添加为嵌入式资源,将其写入临时文件并使用 Assembly.LoadFile() 加载它。

我的问题是有没有其他方法可以在不将其写入临时文件的情况下加载它?我希望将它与 EXE 组合成单个程序集。

谢谢任何建议


对答案的回应:

致:奥列格·伊格纳托夫

嗨,我修改它以加载如下:

static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    Assembly asm = null;
    AppDomain domain = (AppDomain)sender;
    if (args.Name.Contains("System.Data.SQLite"))
    {
        try
        {
            asm = domain.Load(WindowsFormsApplication24.Properties.Resources.System_Data_SQLite);
        }
        catch (Exception ex)
        {
            Form f = new Form();
            TextBox t = new TextBox(); t.Dock = DockStyle.Fill;
            t.Multiline = true; t.ScrollBars = ScrollBars.Both;
            f.Controls.Add(t);
            t.Text = ex.ToString();
            f.ShowDialog();
        }
    }
    return asm;
}

它生成以下异常消息:

System.IO.FileLoadException: Could not load file or assembly 'System.Data.SQLite, Version=1.0.82.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.) (Exception from HRESULT: 0x80131019)
File name: 'System.Data.SQLite, Version=1.0.82.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' ---> System.IO.FileLoadException: Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.) (Exception from HRESULT: 0x80131019)
   at System.Reflection.RuntimeAssembly.nLoadImage(Byte[] rawAssembly, Byte[] rawSymbolStore, Evidence evidence, StackCrawlMark& stackMark, Boolean fIntrospection, SecurityContextSource securityContextSource)
   at System.AppDomain.Load(Byte[] rawAssembly)
   at WindowsFormsApplication24.Program.CurrentDomain_AssemblyResolve(Object sender, ResolveEventArgs args)

这与我以前这样做的结果相同:

byte[] ba = null;
Assembly asm = null;
Assembly curAsm = Assembly.GetExecutingAssembly();
string embeddedResource = "WindowsFormsApplication24.System.Data.SQLite.dll";
using (Stream stm = curAsm.GetManifestResourceStream(embeddedResource))
{
    ba = new byte[(int)stm.Length];
    stm.Read(ba, 0, (int)stm.Length);
    asm = Assembly.Load(ba);
}
return asm;
4

2 回答 2

0

您是否检查过您的目标机器是否安装了 MS Visual C++ 运行时可再发行组件?

如果没有,您可以将 Visual C++ 运行时可再发行组件动态链接到您的项目,因为 System.Data.SQLite 依赖于 Visual C++ 运行时可再发行组件。

.Net 2   - MS Visual C++ 2005 redistributable
.Net 3   - MS Visual C++ 2008 redistributable
.Net 4   - MS Visual C++ 2010 redistributable
.Net 4.5 - MS Visual C++ 2012 redistributable
于 2014-12-08T07:47:56.743 回答
0

您可以处理 AssemblyResolve 事件并使用 AppDomain.Load 方法:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    AppDomain domain = (AppDomain)sender;
    if (args.Name.Contains("YourDll"))
    {
        return domain.Load(WindowsFormsApplication1.Properties.Resources.YourDll);
    }
    return null;
}
于 2013-01-16T05:46:05.413 回答