You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Originally posted by viceroypenguin January 23, 2021
Proposal: Add another project for exposing string and Stream versions of embedded resources. Accessing the resources is something necessary for most source generators, but also for many other projects; you're accessing embedded resources already in the other ThisAssembly.* projects. It would be nice to be able to have compile-time named access to the embedded resources. Thoughts?
The generator would emit a class like the following:
partialclassThisAssembly{publicstaticpartialclassResources{// <summary>Resources\Foo.txt</summary>publicstaticpartialclassFoo{publicstaticstringText=>// return cached string version of the resourcepublicstatic byte[] GetBytes()=>// read bytes from resourcepublicstatic Stream GetStream()=>// get the resource stream}// <summary>MyTemplate.template</summary>
public staticpartial class MyTemplate
{publicstaticstringText=>// return cached string version of the resourcepublicstaticbyte[]GetBytes()=>// read bytes from resourcepublicstatic Stream GetStream()=>// get the resource stream}// <summary>Resources.resx</summary>
public staticpartial class Resources
{publicstaticbyte[]GetBytes()=>// read bytes from resourcepublicstatic Stream GetStream()=>// get the resource stream}
public partial classBranding{// <summary>Main app logo</summary>// <remarks>Branding\Icon.png</remarks>publicstaticpartialclassIcon{publicstaticbyte[]GetBytes()=>// read bytes from resourcepublicstatic Stream GetStream()=>// get the resource stream}}
public partial classData{// <summary>Data\Bar.json</summary>publicstaticpartialclassBar{publicstaticstringText=>// return cached string version of the resourcepublicstaticbyte[]GetBytes()=>// read bytes from resourcepublicstatic Stream GetStream()=>// get the resource stream}}}}
From the example, note:
All EmbeddedResource items get a static class with both GetBytes and GetStream methods for retrieval
Text files (i.e. .txt and .json) also get a Text property with a cached string read from the resource
Only certain file extensions within EmbeddedResources items are defaulted to Text type (therefore getting a generated Text property). By default, this includes .txt;.json, but can be be extended by appending more extensions to the EmbeddedResourceStringExtensions property.
A single EmbeddedResource can also be annotated with the Type="Text" attribute too, to get the Text property just for that item.
The items' target path (that is, %(RelativeDir)\%(Filename)%(Extension) or %(Link) (for linked files) is used by default as the class <summary /> documentation, unless a Comment="...." is provided in the item, in which case that becomes the <summary /> and the former becomes the <remarks>.
The relative directory of the item is used in the class structure to nest classes and avoid name collisions (this is prevented by the solution structure itself too at that point). This is similar to the nesting done by ThisAssembly.Constants
The text was updated successfully, but these errors were encountered:
What if two embedded resources have the same filename? I wonder if a) we should include the path as part of the class structure? and b) if we should optionally include the extension as well?
Discussed in #34
Originally posted by viceroypenguin January 23, 2021
Proposal: Add another project for exposing
string
andStream
versions of embedded resources. Accessing the resources is something necessary for most source generators, but also for many other projects; you're accessing embedded resources already in the other ThisAssembly.* projects. It would be nice to be able to have compile-time named access to the embedded resources. Thoughts?Design
Given the following items in a project:
The generator would emit a class like the following:
From the example, note:
EmbeddedResource
items get a static class with bothGetBytes
andGetStream
methods for retrieval.txt
and.json
) also get aText
property with a cached string read from the resourceEmbeddedResources
items are defaulted toText
type (therefore getting a generatedText
property). By default, this includes.txt;.json
, but can be be extended by appending more extensions to theEmbeddedResourceStringExtensions
property.EmbeddedResource
can also be annotated with theType="Text"
attribute too, to get theText
property just for that item.%(RelativeDir)\%(Filename)%(Extension)
or%(Link)
(for linked files) is used by default as the class<summary />
documentation, unless aComment="...."
is provided in the item, in which case that becomes the<summary />
and the former becomes the<remarks>
.The text was updated successfully, but these errors were encountered: