Import from internal git
This commit is contained in:
15
Core/Helpers/EnumHelper.cs
Normal file
15
Core/Helpers/EnumHelper.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace Core.Helpers;
|
||||
|
||||
public static class EnumHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Retrieved maximum possible value in an enum. In other words, return the number of values contained in the enum
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Enum</typeparam>
|
||||
/// <returns>Number of possible values</returns>
|
||||
public static int GetMaxValue<T>() where T : Enum
|
||||
{
|
||||
var i = Enum.GetValues(typeof(T)).Length;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
85
Core/Helpers/PathHelper.cs
Normal file
85
Core/Helpers/PathHelper.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System.Text;
|
||||
|
||||
namespace Core.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// Provides some path-related actions
|
||||
/// </summary>
|
||||
public static class PathHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Verifies the existence of a path
|
||||
/// </summary>
|
||||
/// <param name="path">Path to check</param>
|
||||
/// <exception cref="FileNotFoundException">If the given path does not exist</exception>
|
||||
public static void CheckPathValidity(string path)
|
||||
{
|
||||
if (!File.Exists(path)) throw new FileNotFoundException($"Could not found file at {path}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a file and all its parents directories if the file does not exists
|
||||
/// </summary>
|
||||
/// <param name="path"></param>
|
||||
public static void CreateFileIfNotExists(string path)
|
||||
{
|
||||
if (!File.Exists(path)) Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts raw text from a file references by given path
|
||||
/// </summary>
|
||||
/// <param name="path">Location of file</param>
|
||||
/// <returns>Raw text</returns>
|
||||
/// <exception cref="FileNotFoundException">If the given path does not exist</exception>
|
||||
public static string TextFrom(string path)
|
||||
{
|
||||
CheckPathValidity(path);
|
||||
return File.ReadAllText(path);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the folder containing the last item of a given path
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="path">The given path</param>
|
||||
/// <returns>The folder containing the last element of the given path</returns>
|
||||
public static string GetFolder(string path)
|
||||
{
|
||||
path = path.Replace("\\", "/");
|
||||
var s = path.Split("/").ToList();
|
||||
s.RemoveAt(s.Count-1);
|
||||
var r = string.Join("/", s) + "/";
|
||||
return r;
|
||||
}
|
||||
|
||||
public static string GetName(string path)
|
||||
{
|
||||
return path.Split("/")[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Concatenates a string representing the path of a folder and a string representing a file name
|
||||
/// The strings come from default arguments.
|
||||
/// </summary>
|
||||
/// <returns>Concatenated path</returns>
|
||||
public static string FullPath(params string[] pathItems)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
foreach (var pi in pathItems)
|
||||
{
|
||||
builder.Append(pi);
|
||||
}
|
||||
return AbsolutePath(builder.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the absolute path based on a relative path
|
||||
/// </summary>
|
||||
/// <param name="relative">relative path</param>
|
||||
/// <returns>Absolute path as string</returns>
|
||||
public static string AbsolutePath(string relative)
|
||||
{
|
||||
return Path.GetFullPath(relative);
|
||||
}
|
||||
}
|
||||
19
Core/Helpers/TypeHelper.cs
Normal file
19
Core/Helpers/TypeHelper.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace Core.Helpers;
|
||||
|
||||
public static class TypeHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Safely cast a less derived object to a more derived object
|
||||
/// </summary>
|
||||
/// <param name="obj">Object to cast</param>
|
||||
/// <typeparam name="TBase">Less derived type</typeparam>
|
||||
/// <typeparam name="TTarget">More derived type</typeparam>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="InvalidOperationException">When data parameter is not a derived of TBase</exception>
|
||||
public static TTarget SafeCast<TBase, TTarget>(TBase obj)
|
||||
{
|
||||
if (obj is not TTarget result)
|
||||
throw new InvalidOperationException($"Cannot cast {typeof(TBase)} to {typeof(TTarget)}");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user