Programmatically Creating Assemblies Using AssemblyBuilder - Verify Contents Using ILDASM
by David Hayden ( Florida .NET Developer )
I had very little free time this past week, but I did find myself learning bits and pieces of things I have never played with before.
One of the more interesting items was AssemblyBuilder. AssemblyBuilder is a class in System.Reflection.Emit that allows you to create assemblies on the fly in your code. My particular interest was in creating a new assembly and then adding resource files to the assembly that I could later open up and grab at a later time.
My particular need was a bit more involved, but here is an example of using it to create an assembly that adds the data and schema from the Categories Table in the Northwind Database and then later getting the data from the assembly. It is not overly complicated and I am still a bit unsure of the most exact and efficient way of doing this, but this worked and is a good place to start.
Saving Table Data and Schema to Text File
First, I grabbed the data from the Categories Table and then saved it and the schema as XML to a couple of files. This is pretty straightforward now that DataTable in ADO.NET 2.0 supports WriteXml and WriteXmlSchema:
DataTable tableData = new DataTable("Data");
string connectionString = "...";
using (SqlConnection connection =
new SqlConnection(ConnectionString))
{
SqlCommand command = connection.CreateCommand();
command.CommandText =
string.Format("SELECT * FROM [Categories]");
connection.Open();
tableData.Load(command.ExecuteReader
(CommandBehavior.CloseConnection));
}
tableData.WriteXml("TableData.xml");
tableData.WriteXmlSchema("TableData.xsd");
Create the Assembly Using AssemblyBuilder and Save The Files in the Assembly
We can then create a new assembly and add these two files, "TableData.xml" and "TableData.xsd", to the assembly for storage. It saves the new assembly as "Categories.dll" in the current directory. Adding the files is as simple as calling AddResourceFile on the instance of AssemblyBuilder. The Save Method saves the assembly to disk.
AppDomain currentDomain = Thread.GetDomain();
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "NorthwindCategories";
AssemblyBuilder builder =
currentDomain.DefineDynamicAssembly(
myAssemblyName,
AssemblyBuilderAccess.RunAndSave);
builder.AddResourceFile("TableXml", "TableData.xml");
builder.AddResourceFile("TableXsd", "TableData.xsd");
builder.Save("Categories.dll");
Using ILDASM to Verify Files are Indeed in the Assembly
To verify the files are indeed in the assembly, run ILDASM and open up the assembly. Click on the assembly's manifest and you will see both the table data and schema:

Programmatically Load the Assembly and Extract the Resources
Now that the data is saved in the assembly and we have verified its existence using ILDASM, I can now dyamically load the assembly in another application and extract the table data.
Assembly assembly = Assembly.LoadFile("Categories.dll");
DataTable table = new DataTable("Data");
table.ReadXmlSchema(assembly.
GetManifestResourceStream("TableXsd"));
table.ReadXml(assembly.
GetManifestResourceStream("TableXml"));
// Bind the table data...
Conclusion
AssemblyBuilder makes it incredibly easy to create assemblies in your code and add resources to it on the fly. ILDASM will allow you to verify the files are actually in the assembly.
Source: David Hayden ( Florida .NET Developer )