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
116 lines (102 loc) · 3.34 KB
/
Utils.cs
File metadata and controls
116 lines (102 loc) · 3.34 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
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using JavaScriptEngineSwitcher.Core.Resources;
namespace JavaScriptEngineSwitcher.Core.Utilities
{
public static class Utils
{
/// <summary>
/// Determines whether the current process is a 64-bit process
/// </summary>
/// <returns>true if the process is 64-bit; otherwise, false</returns>
[MethodImpl((MethodImplOptions)256 /* AggressiveInlining */)]
public static bool Is64BitProcess()
{
#if NETSTANDARD1_3
bool is64Bit = IntPtr.Size == 8;
#else
bool is64Bit = Environment.Is64BitProcess;
#endif
return is64Bit;
}
/// <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.GetTypeInfo().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 a 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 stream = File.OpenRead(path))
using (var reader = new StreamReader(stream, encoding ?? Encoding.UTF8))
{
content = reader.ReadToEnd();
}
return content;
}
/// <summary>
/// Converts a 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))
);
}
}
}