Last active
November 28, 2022 20:38
-
-
Save akunzai/3574a9c5974583dabb930e5d0729cef7 to your computer and use it in GitHub Desktop.
Microsoft.Extensions.FileProviders Extension Methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.Text.Json; | |
| namespace Microsoft.Extensions.FileProviders; | |
| public static class FileProviderExtensions | |
| { | |
| public static Stream ReadAsStream(this IFileProvider fileProvider, string path) | |
| { | |
| return fileProvider.GetFileInfo(path).CreateReadStream(); | |
| } | |
| public static string ReadAsString(this IFileProvider fileProvider, string path) | |
| { | |
| using var reader = new StreamReader(fileProvider.ReadAsStream(path)); | |
| return reader.ReadToEnd(); | |
| } | |
| public static T ReadAsObject<T>(this IFileProvider fileProvider, | |
| string path, | |
| Action<T> configAction = null, | |
| JsonSerializerOptions options = null) | |
| { | |
| using var stream = fileProvider.ReadAsStream(path); | |
| var value = JsonSerializer.Deserialize<T>(stream, options); | |
| configAction?.Invoke(value); | |
| return value; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment