C# 如何在不使用Assembly.LoadFile()的情况下在EXE中合并System.Data.SQLite.DLL

C# 如何在不使用Assembly.LoadFile()的情况下在EXE中合并System.Data.SQLite.DLL,c#,system.data.sqlite,C#,System.data.sqlite,System.Data.SQLite.DLL是一个混合代码DLL。它包含C和C。 我知道如何将其添加为嵌入式资源,将其写入临时文件并使用Assembly.LoadFile()加载它 我的问题是,有没有其他方法可以在不将其写入临时文件的情况下加载它? 我希望将它与EXE组合成一个程序集 谢谢你的建议 对答案的回答: 收件人:Oleg Ignatov 您好,我将其修改为如下加载: static System.Reflection.Assembly CurrentDomain_AssemblyR

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

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

谢谢你的建议


对答案的回答:

收件人:Oleg Ignatov

您好,我将其修改为如下加载:

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;
}
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;
它会生成以下内容的异常消息:

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)
这与我之前所做的结果相同,如下所示:

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;
}
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;

您可以处理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;
}

你检查你的目标机器是MS VisualC++运行时可重新分配的安装吗?BR/>BR/>如果不是,你可以动态链接VisualC++运行时可重新分配到你的项目中,Stask.DATA SQL LITE依赖VisualC++运行时重新分配。

.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

您好,我在问题中添加了详细信息。我在实现此代码后发布了结果。我发现了一个例外,你知道吗?我也面临着类似的问题