forked from dotnet/try
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeExtensions.cs
More file actions
39 lines (32 loc) · 1.24 KB
/
Copy pathTypeExtensions.cs
File metadata and controls
39 lines (32 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.IO;
using System.Linq;
namespace MLS.Agent.Tools
{
public static class TypeExtensions
{
public static string ReadManifestResource(this Type type, string resourceName)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}
if (string.IsNullOrWhiteSpace(resourceName))
{
throw new ArgumentException("Value cannot be null or whitespace.", nameof(resourceName));
}
var assembly = type.Assembly;
var assemblyResourceName = assembly.GetManifestResourceNames().First(s => s.Contains(resourceName));
if (string.IsNullOrWhiteSpace(assemblyResourceName))
{
throw new InvalidOperationException($"Cannot locate resource {resourceName} in {assembly}");
}
using (var reader = new StreamReader(assembly.GetManifestResourceStream(assemblyResourceName)))
{
return reader.ReadToEnd();
}
}
}
}