Dynamically creating satellite assemblies with embedded resources
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.

Post a new message to this list...

Notre Poubelle
I'd like to create a satellite assembly dynamically, embedding rather than
linking resource files. I know I can use the assembly linker (al.exe) to
embed a .resources file, but I'd like to do it programmatically, using
something like the AssemblyBuilder class.

This is the same question that Nick Carter asked before. I've pasted his
post below, it case it clarifies things for anyone.
------ Nick Cases original post -----
al.exe has a /embed switch which allows me to create an assembly and embed a
resource in it. I want to duplicate this behaviour using the AssemblyBuilder
class. AssemblyBuilder has two methods, AddResourceFile and DefineResource,
which allow me to add resources to an assembly but AFAICS the files which
are specified using these methods are simply referenced - they are not
embedded. The resulting assembly still needs the .resource file to be
present at runtime. How can I use AssemblyBuilder to add resources to an
assembly where the resources are embedded (i.e. they have their
ResourceLocation.Embedded and ResourceLocation.ContainedInManifestFile flags
set just like al.exe does when using the /embed switch) ?

Reply to this message...
 
    
Nick Carter
Notre,

The solution to this problem is to use ModuleBuilder.DefineResource to
define the resource. The resulting resource has the
ResourceLocation.Embedded and ResourceLocation.ContainedInManifestFile flags
set. AssemblyBuilder.DefineResource and AssemblyBuilder.AddResourceFile do
not set these flags.

Nick

"Notre Poubelle" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Notre Poubelle
Thanks Nick, but I'm still missing something. I've copied a bit of C# code
I've written to try to accomplish this. As you can see, I'm using the
ModuleBuilder.DefineResource method, but what I'm finding is that if I don't
include the .resources file in the same location as the .dll, then the
runtime doesn't find the satellite assembly contents correctly. Looking at
the generated assembly using ILDASM suggests that I'm still linking rather
than embedding the resource file.

            string myAsmFileName = "WriteSatelliteAssembly.resources.dll";
            string path;
    
            path = AppDomain.CurrentDomain.BaseDirectory + "fr-FR";
            AppDomain myDomain = Thread.GetDomain();
            AssemblyName myAsmName = new AssemblyName();
            myAsmName.Name = "WriteSatelliteAssembly.resources";
            myAsmName.CodeBase = path;
            myAsmName.CultureInfo = new CultureInfo("fr");

            AssemblyBuilder myAsmBuilder = myDomain.DefineDynamicAssembly(
                myAsmName,
                AssemblyBuilderAccess.RunAndSave, path);

            //embed the resources
            ModuleBuilder myModuleBuilder =
                myAsmBuilder.DefineDynamicModule("TempModule",
                "WriteSatelliteAssembly.MyResource2.fr.resources");
            IResourceWriter rw =
                myModuleBuilder.DefineResource("WriteSatelliteAssembly.MyResource2.fr.resources",
                "Description",ResourceAttributes.Public);
            
            rw.AddResource("resName","My (dynamic) resource value.");
            rw.AddResource("resName2","My (dynamic) second resource value.");

            myAsmBuilder.Save(myAsmFileName);

Reply to this message...
 
    
Nick Carter
I suspect that this is a module naming problem. It looks as if your module
name is not the same as the assembly name. AssemblyBuilder.Save treats
modules differently when they have the same name as the assembly. Check the
documentation on AssemblyBuilder.Save for an explanation.

Nick

"Notre Poubelle" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.AppDomain
System.Globalization.CultureInfo
System.Reflection.AssemblyName
System.Reflection.Emit.AssemblyBuilder
System.Reflection.Emit.AssemblyBuilderAccess
System.Reflection.Emit.ModuleBuilder
System.Reflection.ResourceAttributes
System.Reflection.ResourceLocation
System.Resources.IResourceWriter
System.Threading.Thread




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com