资源文件(的.resx)与反射来访问嵌入的资源 [英] Resource file (.resx) vs reflection to access an embedded resource

查看:214
本文介绍了资源文件(的.resx)与反射来访问嵌入的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能失去了一些东西很简单,在这里,但什么是使用反射来检索包含该资源在相同的组件嵌入资源的利益而不是简单地通过一个.resx文件中检索它?我看到这个了很多,但没有得到它 - 是有没有理由使用 Assembly.GetExecutingAssembly()GetManifestResourceStream(资源)相比,RESX文件 Resources.resource ?即使是微软做的:。如何嵌入和访问资源

我的意思正是:假设我有一个程序集 MyAssembly程序包含嵌入的资源的Config.xml 。该组件具有 MyClass的实现返回所述资源作为字符串的方法:

 公共字符串GetConfigXML()//返回Config.xml中的内容作为一个字符串
 

通常情况下,我认为这实施这样的,使用反射来获取资源:

 公共字符串GetConfigXML()
{
    流xmlStream = Assembly.GetExecutingAssembly()GetManifestResourceStream(MyAssembly.Config.xml)。
    XML字符串= GetStringFromStream(xmlStream);
    返回XML;
}
 

为什么要用 GetManifestResourceStream()时,您可以:

  1. 在一个资源文件( Resource.resx )添加到Visual Studio中的 MyAssembly程序项目;
  2. 的Config.xml 添加到资源的文件;
  3. 获得的Config.xml 在一个更简单的方法内容: XML字符串= Resource.Config;

我不知道怎么工作室视觉内部处理.resx文件,但我怀疑它只是简单复制的资源到.resx文件(在这种情况下,你最终会与重复的资源)。我认为它不使用反射内部或者,为什么不干脆用.resx文件在这样的情况下,这似乎更高性能友好给我?

解决方案
  

但什么是使用反射来检索嵌入资源的利益

在共同利益的背后任何理由将数据从一种格式转换为另一种。速度,速度,速度和便利性。

XML是pretty的体面的格式,以保持存储在你的资源,你将有一个很好的保证,你仍然可以从10年后找回原来的资源时,原来的迷失在了时间的迷雾和一对夫妇的机器的变化没有良好的备份。但是,这是一个相当苏茨基格式有从,XML是非常详细和定位的片段,需要从文件开始阅读阅读。

当Resgen.exe编译.xml文件到.resource文件消失的问题。二进制格式是适合被链接到您的程序集元数据,并包含在资源的原始字节。而被直接映射到内存中,当你的程序集加载,无需另找文件并打开它,阅读它并转换数据。很大的区别。

确实使用资源设计来避免()直接使用GetManifestResourceStream。然而,更多的便利。

I may be missing something very simple here, but what's the benefit of using reflection to retrieve an embedded resource from the same assembly that contains the resource as opposed to simply retrieving it via an .resx file? I see this a lot but don't get it - is there a reason to use Assembly.GetExecutingAssembly().GetManifestResourceStream(resource) compared to resx file Resources.resource? Even Microsoft does it: How to embed and access resources.

What I mean exactly: suppose I have an assembly MyAssembly that contains an embedded resource Config.xml. The assembly has MyClass that implements a method that returns said resource as a string:

public string GetConfigXML() // returns the content of Config.xml as a string

Often, I see this implemented like this, using reflection to retrieve the resource:

public string GetConfigXML()
{
    Stream xmlStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyAssembly.Config.xml"); 
    string xml = GetStringFromStream(xmlStream);
    return xml;
}

Why use GetManifestResourceStream() when you can:

  1. add a resource file (Resource.resx) to the MyAssembly project in Visual Studio;
  2. add Config.xml to the resource's 'Files';
  3. get the content of Config.xml in a much simpler way: string xml = Resource.Config;

I don't know how Visual Studio handles .resx files internally, but I doubt it simply copies the resource into the .resx file (in which case you'd end up with duplicated resources). I assume it doesn't use reflection internally either, so why not simply use .resx files in situations like this, which seems much more performance-friendly to me?

解决方案

but what's the benefit of using reflection to retrieve an embedded resource

The common benefit that's behind any reason to convert data from one format to another. Speed, speed, speed and convenience.

XML is a pretty decent format to keep your resources stored in. You'll have a very good guarantee that you can still retrieve the original resource 10 years from now when the original got lost in the fog of time and a couple of machine changes without good backups. But it is quite a sucky format to have to read from, XML is very verbose and locating a fragment requires reading from the start of the file.

Problems that disappear when Resgen.exe compiles the .xml file into a .resource file. A binary format that's fit to be linked into your assembly metadata and contains the original bytes in the resource. And is directly mapped into memory when your assembly is loaded, no need to find another file and open it, read it and convert the data. Big difference.

Do use the Resource Designer to avoid having to use GetManifestResourceStream() directly. Yet more convenience.

这篇关于资源文件(的.resx)与反射来访问嵌入的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆