Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build resource assemblies with AssemblyBuilder

Scenario: I want to create satellite assemblies which are resource assemblies. This assembly has only compiled resources in it (ResourceWriter). The goal is to create resource assemblies outside of VS and add them to the application and this in my C# application

Atm I´m using AssemblyBuilder to create the Assembly. It works but there is no information about the assembly stored. No cultureinfo, key or anything else. The assembly isn´t taken by the app with an Missing Manfifest Resource Exception. :(

If possible i want to stay with AssemblyBuilder or using CodeDomProvider.

Question: What is necessary to be able to add new satelite assembly to my app? Is it enough to have a folder with the culture (en-US) and an assembly with the en-US resources in there?

Question2: Is it possible to give some meta information like Version, Culture to the assembly?

Question3: Is ist enough to add the resources to the assembly?

Code:

AssemblyBuilder builder = AppDomain.CurrentDomain.DefineDynamicAssembly(
                        assemblyName, AssemblyBuilderAccess.RunAndSave, resourceFolder);

builder.AddResourceFile(name, assemblyFileName);
builder.Save(assemblyName.Name);

For any kind of help i would be grateful. Thanks in advance

like image 631
Orri Avatar asked Dec 26 '11 10:12

Orri


People also ask

What is a dynamic assembly?

Dynamic assemblies are those assemblies which are not stored on the disk before execution in fact after execution they get stored on the disk. When .NET runtime calls them they are directly loaded from the memory not from the disk.

Which CLR API can be used to create dynamic assemblies System assembly generate system runtime create system reflection emit system dynamic?

Remarks. A dynamic assembly is an assembly that is created using the Reflection Emit APIs. You can use AssemblyBuilder to generate dynamic assemblies in memory and execute their code during the same application run.


1 Answers

Found the solution.

Source: http://www.dotnet247.com/247reference/msgs/58/290731.aspx

Explanation: First It seems the AssemblyBuilder only link the resource to the assembly, its not embedded. Second the resource has to be in Module to be seen by the main assembly. (I dislike to crate the Resource within the module but there seems to be no way to embedded already existing resource)

the Code:

        string myAsmName = "WriteSatelliteAssembly.resources";
        string myAsmFileName = myAsmName + ".dll";
        string resourceName = "WriteSatelliteAssembly.MyResource2.fr.resources";
        string path;

        path = AppDomain.CurrentDomain.BaseDirectory + "fr-FR";
        AppDomain appDomain = Thread.GetDomain();
        AssemblyName asmName = new AssemblyName();
        asmName.Name = myAsmName;
        asmName.CodeBase = path;
        asmName.CultureInfo = new CultureInfo("fr");

        AssemblyBuilder myAsmBuilder = appDomain.DefineDynamicAssembly(
            asmName,
            AssemblyBuilderAccess.RunAndSave, path);

        **ModuleBuilder** myModuleBuilder =
            myAsmBuilder.DefineDynamicModule(myAsmFileName,
            myAsmFileName);
        **IResourceWriter** rw =
            myModuleBuilder.DefineResource(resourceName,
            "My Description",ResourceAttributes.Public);

        rw.AddResource("resName","My (dynamic) resource value.");
        rw.AddResource("resName2","My (dynamic) second resource value.");

        myAsmBuilder.Save(myAsmFileName);

Proof shortend:

Linked resource in assembly

.file nometadata 'TestProjectResourceManager.Properties.Resources.en-US.resources'
    .hash = (57 DD 82 37 9E B3 BA 8A 27 D0 69 90 37 67 22 23   // W..7....'.i.7g"#
             A0 1C F7 47 )                                     // ...G
.mresource public TestProjectResourceManager.Properties.Resources
{
  .file 'TestProjectResourceManager.Properties.Resources.en-US.resources' at 0x00000000
}

embedded resource in assembly

.mresource public 'TestProjectResourceManager.Properties.Resources.en-US.resources'
{
  // Offset: 0x00000000 Length: 0x000000EE
}
.module TestProjectResourceManager_2.resources.dll

Have fun

like image 97
Orri Avatar answered Oct 13 '22 02:10

Orri