Рфеакторинг метода расширения получения файла из сборки

This commit is contained in:
Степанов Дмитрий 2024-02-01 16:02:19 +05:00
parent 96a8c3fbd3
commit 3865c330c2

View File

@ -1,30 +1,50 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure
{
public static class AssemblyExtensions
{
public static async Task<Stream> GetTemplateCopyStreamAsync(this Assembly assembly, string templateName, CancellationToken cancellationToken)
{
var resourceName = assembly
.GetManifestResourceNames()
.FirstOrDefault(n => n.EndsWith(templateName))!;
public static class AssemblyExtensions
{
public static Stream GetTemplateCopyStream(this Assembly assembly, string templateName)
{
var resourceName = assembly
.GetManifestResourceNames()
.FirstOrDefault(n => n.EndsWith(templateName));
using var stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(resourceName)!;
if (string.IsNullOrWhiteSpace(resourceName))
throw new ArgumentNullException(nameof(resourceName));
var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream, cancellationToken);
memoryStream.Position = 0;
using var stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(resourceName);
return memoryStream;
}
}
}
var memoryStream = new MemoryStream();
stream?.CopyTo(memoryStream);
memoryStream.Position = 0;
return memoryStream;
}
[Obsolete]
public static async Task<Stream> GetTemplateCopyStreamAsync(this Assembly assembly,
string templateName,
CancellationToken cancellationToken)
{
var resourceName = assembly
.GetManifestResourceNames()
.FirstOrDefault(n => n.EndsWith(templateName))!;
using var stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(resourceName)!;
var memoryStream = new MemoryStream();
await stream.CopyToAsync(memoryStream, cancellationToken);
memoryStream.Position = 0;
return memoryStream;
}
}
}