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;