This message was discovered on microsoft.public.dotnet.framework.clr.
| 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) ?
|
|
| |
| |
| 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]
|
|
| |
| |
| 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);
|
|
| |
| | |
|
|
|
|
|
|