从加载的 DLL 而不是 RESX 文件中读取 RESX 内容

发布于 2024-11-18 18:22:30 字数 816 浏览 4 评论 0原文

我需要非常清楚这一点。我不想读取 myfile.resx 文件。我正在尝试从 myresourcenamespace.myfile.dll 读取内容。

我的希望是通过访问预加载到 DLL 中的内容来创建包含在 RESX 内容中的 KVP 的字典。我当前的解决方案过于依赖现有的文件和大量复制以确保文件位于需要的位置。此时 DLL 更加可靠。

我已经能够通过“new resourcemanager(myassembletype)”访问程序集,但我陷入了这一点。我似乎无法从记忆中读取实际内容。当我尝试从资源管理器以流的形式访问内容时,我不断遇到 MissingManifestException。

这是我的成功代码:

var myType = Type.GetType("ViewRes.StaticMessages", true);
var myResManager = new ResourceManager("StaticMessages", 
                           System.Reflection.Assembly.GetAssembly(myType));

这是我的失败代码:

using (var fileReader = new ResXResourceSet(
                           myResManager.GetStream(myResManager.BaseName)))
{
//.... code read here
}

ResXResourceSet 抛出 MissingManifestException。我已经尝试了我能想到的一切。

I need to be really clear on this. I am not trying to read the myfile.resx file. I am trying to read the content from the myresourcenamespace.myfile.dll.

My hopes is to create a dictionary of my KVPs contained within the RESX content, by accessing that which is preloaded into the DLLs. My current solution depends too much on the files exisiting and a bunch of copying to insure that files are where they need to be. The DLLs are more reliable at this point.

I have been able to gain access to the assembly via "new resourcemanager(myassemblytype)", but I am stuck at this point. I cannot seem to read the actual content from memory. I continually run up against MissingManifestException when I try and access the content as a stream from the resource manager.

Here is my successful code:

var myType = Type.GetType("ViewRes.StaticMessages", true);
var myResManager = new ResourceManager("StaticMessages", 
                           System.Reflection.Assembly.GetAssembly(myType));

Here is my failure code:

using (var fileReader = new ResXResourceSet(
                           myResManager.GetStream(myResManager.BaseName)))
{
//.... code read here
}

The ResXResourceSet throws the MissingManifestException. I have tried everything I can think of.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉墨 2024-11-25 18:22:30

而不是:

var myType = Type.GetType("ViewRes.StaticMessages", true);
var myResManager = new ResourceManager("StaticMessages", 
                       System.Reflection.Assembly.GetAssembly(myType));

尝试这样:

var myType = Type.GetType("ViewRes.StaticMessages", true);
var myResManager = new ResourceManager("StaticMessages", myType.Assembly);
// you could specify the desired culture for which you would like to get the
// resource values
var culture = CultureInfo.InvariantCulture; 
var resourceSet = myResManager.GetResourceSet(culture, true, true);

现在你可以循环:

foreach (DictionaryEntry item in resourceSet)
{
    var key = item.Key;
    var value = item.Value;
}

Instead of:

var myType = Type.GetType("ViewRes.StaticMessages", true);
var myResManager = new ResourceManager("StaticMessages", 
                       System.Reflection.Assembly.GetAssembly(myType));

Try like this:

var myType = Type.GetType("ViewRes.StaticMessages", true);
var myResManager = new ResourceManager("StaticMessages", myType.Assembly);
// you could specify the desired culture for which you would like to get the
// resource values
var culture = CultureInfo.InvariantCulture; 
var resourceSet = myResManager.GetResourceSet(culture, true, true);

now you can loop:

foreach (DictionaryEntry item in resourceSet)
{
    var key = item.Key;
    var value = item.Value;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文