I’m actually looking for solution merge all DLL and EXE into single file.
I have asked a question at here:
and I received suggestion that I can link the DLL as embed resource, then write the embed DLL file into memory and use DLLImport to import the DLL.
I followed the instructions here:
and below is what I have done:
[DllImport("System.Data.SQLite.dll")]
public static SQLiteConnection sqLiteConnection1 = new SQLiteConnection();
public Form1()
{
ResourceExtractor.ExtractResourceToFile("MyApp.System.Data.SQLite.dll", "System.Data.SQLite.dll");
}
public static class ResourceExtractor
{
public static void ExtractResourceToFile(string resourceName, string filename)
{
if (!System.IO.File.Exists(filename))
using (System.IO.Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
using (System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create))
{
byte[] b = new byte[s.Length];
s.Read(b, 0, b.Length);
fs.Write(b, 0, b.Length);
}
}
}
but Visual Studio says that this block creates an error:
[DllImport("System.Data.SQLite.dll")]
public static SQLiteConnection sqLiteConnection1 = new SQLiteConnection();
Error 1 Attribute ‘DllImport’ is not valid on this declaration type. It is only valid on ‘method’ declarations.
How to declare the type inside that DLL?
Thanks you very much.
DllImportis only for native DLLs.On embedding managed DLLs you have several options:
For howto see here and here
OR
it can embed and merge among other things (no need to change your source code)
OR
mark all needed dependencies as “embedded resource” – this way they are included in the EXE file… you need to setup an
AssemblyResolvehandler which at runtime reads from Resources and returns the needed DLLs to the .NET runtime…On using a type from such an Assembly see these links (they including reference material AND some sample code etc.):