I may miss something very simple here, but what is the use of using reflection to extract an embedded resource from the same assembly that contains the resource , rather than just extracting it through a .resx file? I see this a lot, but I donβt understand - is there a reason to use Assembly.GetExecutingAssembly().GetManifestResourceStream(resource) compared to the resx Resources.resource file? Even Microsoft does this: How to implement and access resources .
What I mean exactly: suppose I have a MyAssembly assembly that contains the Config.xml built-in resource. The collection has MyClass , which implements a method that returns the specified resource as a string:
public string GetConfigXML()
Often I see that this is implemented like this, using reflection to retrieve a resource:
public string GetConfigXML() { Stream xmlStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyAssembly.Config.xml"); string xml = GetStringFromStream(xmlStream); return xml; }
Why use GetManifestResourceStream() if you can:
- Add a resource file (
Resource.resx ) to the MyAssembly project in Visual Studio; - Add
Config.xml to the Files resource; - getting the contents of
Config.xml much simpler: string xml = Resource.Config;
I do not know how Visual Studio processes .resx files internally, but I doubt that it simply copies the resource to the .resx file (in this case, you will get duplicated resources). I assume that it also does not use reflection inside, so why not just use .resx files in situations that seem more convenient to me?
source share