反思 |发出 |动态资源生成 - 使用 IResourceWriter.AddResource(key,value) 添加的资源值无法读取
Reflection | Emit | Dynamic Resource Generation - Added resource values using IResourceWriter.AddResource(key,value) cannot read
我正在尝试使用一些资源生成程序集文件。我正在使用 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");
}
资源管理器可以加载资源,但它没有任何键值对。
生成的程序集包含资源值:
所以问题在ResourceManager
.
The ResourceManager class retrieves resources from a binary .resources
file that is embedded in an assembly or from a standalone .resources
file
并且您正在创建的资源未嵌入二进制 .resources 文件
尝试 ResourceReader
,这更适合您的情况
using (var reader = new ResourceReader(asm.GetManifestResourceStream(resourceName)))
{
foreach (DictionaryEntry entry in reader)
{
}
}
我正在尝试使用一些资源生成程序集文件。我正在使用 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");
}
资源管理器可以加载资源,但它没有任何键值对。
生成的程序集包含资源值:
所以问题在ResourceManager
.
The ResourceManager class retrieves resources from a binary .resources file that is embedded in an assembly or from a standalone .resources file
并且您正在创建的资源未嵌入二进制 .resources 文件
尝试 ResourceReader
,这更适合您的情况
using (var reader = new ResourceReader(asm.GetManifestResourceStream(resourceName)))
{
foreach (DictionaryEntry entry in reader)
{
}
}