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

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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification