Code Monkey home page Code Monkey logo

embeddingresourcecsharp's Introduction

EmbedResourceCSharp

This is a C# Source Generator. This let you embed files in your application. You do not need to use Assembly.GetManifestResourceStream anymore.

How to use

Install

dotnet add package EmbedResourceCSharp

Add only 1 package to your C# project.

Embedding file

Provide that there are some files like below.

  • projectFolder/
    • Example.csproj
    • ExampleProgram.cs
    • resourceFileA.txt
namespace Example
{
    // partial methods require partial class/struct!
    public partial class ExampleClass
    {
        /*
            The relative file path from C# project folder should be specified.
            The return value type must be System.ReadOnlySpan<byte>.
            No parameter must exist.
            The method must be static and partial.
            The accessibility of the method does not matter.
        */
        [EmbedResourceCSharp.FileEmbed("resourceFileA.txt")]
        private static partial System.ReadOnlySpan<byte> GetFileContentA();
    }
}

You can get file content byte sequence with static partial method System.ReadOnlySpan<byte> GetFileContentA.

Embedding files under specific folder

Provide that there are some files like below.

  • projectFolder/
    • Example2.csproj
    • ExampleProgram.cs
  • folderB/
    • resourceA.txt
    • resourceB.txt
    • folderB_C/
      • resourceC.txt
    • resourceD.csv
namespace Example2
{
    // partial methods require partial class/struct!
    public partial class ExampleClass
    {
        /*
            The relative folder path from C# project folder should be specified. The folder path should end with slash or backslash.
            The return value type must be System.ReadOnlySpan<byte>.
            One parameter must exist and its type must be System.ReadOnlySpan<char>. The parameter name does not matter.
            The method must be static and partial.
            The accessibility of the method does not matter.
        */
        [EmbedResourceCSharp.FolderEmbed("../folderB/", "*.txt")]
        private static partial System.ReadOnlySpan<byte> GetResouceFileContent(System.ReadOnlySpan<char> path);

        public static void Main()
        {
            // Specify relative path from the folder.
            var aContent = GetResouceFileContent("resourceA.txt");
            var bContent = GetResouceFileContent("resourceB.txt");
            var cContent = GetResouceFileContent("folderB_C/resourceC.txt");
            // var dContent = GetResouceFileContent("resourceD.csv");
            // Above method call throws an FileNotFoundException!
        }
    }
}

You can include all files under the target folder recursively. You can filter file with search pattern.

embeddingresourcecsharp's People

Contributors

pcysl5edgo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

embeddingresourcecsharp's Issues

Setting AnalysisLevel to 7.0 results in excessive build times

If you set the AnalysisLevel in the csproj to 7.0, the build times grow seemingly exponentially with the number of files in the project (not the number of embedded files, mind you). Forcing this to 0.0 makes the builds instant again as a workaround.

I timed my project's build time, and it came out at over 3 hours on GH Actions, and disabling the anylzer results in it taking seconds again.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.