我正在尝试使用一些资源生成一个程序集文件。我System.Reflection.Emit.ModuleBuilder
用来定义资源。AssemblyBuilder
保存程序集而不给出任何错误,但是当我尝试读取资源时,ResourceManager
它没有任何条目ResourceSets
。我不确定AssemblyBuilder
是否保存添加的资源或我的资源读取代码有问题。
汇编生成代码:
public void GenerateAsm()
{
string myAsmName = "MyAssembly.resources";
string myAsmFileName = myAsmName + ".dll";
string resourceName = "MyAssembly.Resource.fr.resources";
AppDomain appDomain = Thread.GetDomain();
AssemblyName asmName = new AssemblyName();
asmName.Name = myAsmName;
AssemblyBuilder myAsmBuilder = appDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder myModuleBuilder = myAsmBuilder.DefineDynamicModule(myAsmFileName,myAsmFileName);
IResourceWriter rw = myModuleBuilder.DefineResource(resourceName, "My Description", ResourceAttributes.Public);
rw.AddResource("ResKey1", "Resource value 1.");
rw.AddResource("ResKey2", "Resource value 2.");
myAsmBuilder.Save(myAsmFileName);
}
资源读取代码:
public void ReadResource()
{
ResourceManager rm = new ResourceManager("MyAssembly.Resource.fr.resources", Assembly.LoadFrom("MyAssembly.resources.dll"));
string greeting = rm.GetString("ResKey1");
}
资源管理器可以加载资源,但它没有任何键值对。