SmallInjectorDemo/Helper.cs
Holger Boerchers 1d2fca6ddc updated
2018-08-15 08:46:46 +02:00

55 lines
1.9 KiB
C#

using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
namespace SmallInjectorDemo
{
/// <summary>
/// Static helper class for generating random numbers.
/// </summary>
public static class Helper
{
/// <summary>
/// Return cryptographic stable random integer.
/// </summary>
/// <param name="min">Lower border of result.</param>
/// <param name="max">Upper border of result.</param>
/// <returns>Random integer.</returns>
public static int NewRandomInteger(int min, int max)
{
// The random number provider.
using (var rand = new RNGCryptoServiceProvider())
{
var scale = uint.MaxValue;
while (scale == uint.MaxValue)
{
// Get four random bytes.
var four_bytes = new byte[4];
rand.GetBytes(four_bytes);
// Convert that into an uint.
scale = BitConverter.ToUInt32(four_bytes, 0);
}
// Add min to the scaled difference between max and min.
return (int) (min + (max - min) * (scale / (double) uint.MaxValue));
}
}
/// <summary>
/// Write method string.
/// </summary>
/// <param name="id">id of the object</param>
/// <param name="memberName">member name</param>
/// <param name="filepath">file path</param>
/// <returns></returns>
public static string WriteMethodString(int id, [CallerMemberName] string memberName = null, [CallerFilePath] string filepath = null)
{
var classname = Path.GetFileNameWithoutExtension(filepath);
return $"{classname}.{memberName?.TrimStart('.')}".PadRight(30) + $"Id: {id}";
}
}
}