forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
150 lines (131 loc) · 4.21 KB
/
Utils.cs
File metadata and controls
150 lines (131 loc) · 4.21 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
namespace JavaScriptEngineSwitcher.Core.Utilities
{
using System;
using System.IO;
using System.Reflection;
using System.Text;
using Resources;
public static class Utils
{
/// <summary>
/// Gets a content of the embedded resource as string
/// </summary>
/// <param name="resourceName">Resource name</param>
/// <param name="type">Type from assembly that containing an embedded resource</param>
/// <returns>Сontent of the embedded resource as string</returns>
public static string GetResourceAsString(string resourceName, Type type)
{
Assembly assembly = type.Assembly;
return GetResourceAsString(resourceName, assembly);
}
/// <summary>
/// Gets a content of the embedded resource as string
/// </summary>
/// <param name="resourceName">Resource name</param>
/// <param name="assembly">Assembly that containing an embedded resource</param>
/// <returns>Сontent of the embedded resource as string</returns>
public static string GetResourceAsString(string resourceName, Assembly assembly)
{
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null)
{
throw new NullReferenceException(
string.Format(Strings.Resources_ResourceIsNull, resourceName));
}
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
/// <summary>
/// Gets text content of the specified file
/// </summary>
/// <param name="path">File path</param>
/// <param name="encoding">Content encoding</param>
/// <returns>Text content</returns>
public static string GetFileTextContent(string path, Encoding encoding = null)
{
if (!File.Exists(path))
{
throw new FileNotFoundException(
string.Format(Strings.Common_FileNotExist, path), path);
}
string content;
using (var file = new StreamReader(path, encoding ?? Encoding.UTF8))
{
content = file.ReadToEnd();
}
return content;
}
/// <summary>
/// Creates instance by specified full type name
/// </summary>
/// <param name="fullTypeName">Full type name</param>
/// <typeparam name="T">Target type</typeparam>
/// <returns>Instance of type</returns>
internal static T CreateInstanceByFullTypeName<T>(string fullTypeName) where T : class
{
if (string.IsNullOrWhiteSpace(fullTypeName))
{
throw new ArgumentNullException(Strings.Common_ValueIsEmpty);
}
string typeName;
string assemblyName;
Assembly assembly;
int commaPosition = fullTypeName.IndexOf(',');
if (commaPosition != -1)
{
typeName = fullTypeName.Substring(0, commaPosition).Trim();
if (string.IsNullOrEmpty(typeName))
{
throw new EmptyValueException(Strings.Common_TypeNameIsEmpty);
}
assemblyName = fullTypeName.Substring(commaPosition + 1,
fullTypeName.Length - (commaPosition + 1)).Trim();
if (string.IsNullOrEmpty(assemblyName))
{
throw new EmptyValueException(Strings.Common_AssemblyNameIsEmpty);
}
assembly = Assembly.Load(assemblyName);
}
else
{
typeName = fullTypeName;
assembly = typeof(Utils).Assembly;
assemblyName = assembly.FullName;
}
object instance = assembly.CreateInstance(typeName);
if (instance == null)
{
throw new NullReferenceException(string.Format(Strings.Common_InstanceCreationFailed,
typeName, assemblyName));
}
return (T)instance;
}
/// <summary>
/// Converts value of source enumeration type to value of destination enumeration type
/// </summary>
/// <typeparam name="TSource">Source enumeration type</typeparam>
/// <typeparam name="TDest">Destination enumeration type</typeparam>
/// <param name="value">Value of source enumeration type</param>
/// <returns>Value of destination enumeration type</returns>
public static TDest GetEnumFromOtherEnum<TSource, TDest>(TSource value)
{
string name = value.ToString();
var destEnumValues = (TDest[])Enum.GetValues(typeof(TDest));
foreach (var destEnum in destEnumValues)
{
if (string.Equals(destEnum.ToString(), name, StringComparison.OrdinalIgnoreCase))
{
return destEnum;
}
}
throw new InvalidCastException(
string.Format(Strings.Common_EnumValueConversionFailed,
name, typeof(TSource), typeof(TDest))
);
}
}
}