1821 lines
51 KiB
C#
1821 lines
51 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Runtime.Serialization;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using System.Xml;
|
|
using AutoMapper;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json.Serialization;
|
|
using xCommons.Constants;
|
|
using xCommons.Helpers;
|
|
using xExceptions.Attributes;
|
|
using xExceptions.Constants;
|
|
|
|
namespace xCommons.Extensions
|
|
{
|
|
public static partial class CommonExtensions
|
|
{
|
|
/// <summary>
|
|
/// Conert XMLNode to String ...
|
|
/// </summary>
|
|
/// <param name="node"></param>
|
|
/// <param name="indentation"></param>
|
|
/// <returns></returns>
|
|
public static string ToString(this XmlNode node, int indentation = 2)
|
|
{
|
|
//
|
|
using (var sw = new StringWriter())
|
|
{
|
|
//
|
|
using (var xw = new XmlTextWriter(sw))
|
|
{
|
|
xw.Formatting = System.Xml.Formatting.Indented;
|
|
xw.Indentation = indentation;
|
|
node.WriteContentTo(xw);
|
|
}
|
|
|
|
//
|
|
return sw.ToString();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a Json String to Dictionary of String Mappings
|
|
/// </summary>
|
|
/// <param name="source">Json String</param>
|
|
/// <returns></returns>
|
|
public static Dictionary<string, string> JSONToDictionary(this string source)
|
|
{
|
|
//
|
|
try
|
|
{
|
|
return JsonConvert.DeserializeObject<Dictionary<string, string>>(source);
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert properties of specific Class instance to Dictionary ...
|
|
/// </summary>
|
|
/// <param name="source">class instance</param>
|
|
/// <returns></returns>
|
|
public static Dictionary<string, string> ToDictionary<T>(this T source) where T : class
|
|
{
|
|
//
|
|
var result = new Dictionary<string, string>();
|
|
|
|
//
|
|
if (source.IsNull())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
PropertyInfo[] propertyInfos = source.GetType().GetProperties();
|
|
if (!propertyInfos.HasChild())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
foreach (PropertyInfo propertyInfo in propertyInfos)
|
|
{
|
|
result.Add(
|
|
propertyInfo.Name,
|
|
propertyInfo
|
|
.GetValue(source, null)
|
|
.ToString()
|
|
);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// run specific task and retrieve result ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
public static void RunTask(this Task source)
|
|
{
|
|
source.
|
|
GetAwaiter()
|
|
.GetResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// run specific task and retrieve result ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
public static T RunTask<T>(this Task<T> source)
|
|
{
|
|
return source.
|
|
GetAwaiter()
|
|
.GetResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert XML String to Json Object ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static string ToJSON(this string source)
|
|
{
|
|
//
|
|
var result = string.Empty;
|
|
try
|
|
{
|
|
//
|
|
result = XMLToJSONHelper.ToJSON(source);
|
|
|
|
//
|
|
// Handle Removing Additional Values ...
|
|
result = result.Replace("s:", "").Replace("a:", "");
|
|
}
|
|
catch { }
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert object to Json ...
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public static string ToJSON<T>(this T value)
|
|
where T : class
|
|
{
|
|
//
|
|
if (value.IsNull())
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
//
|
|
var setting = new JsonSerializerSettings
|
|
{
|
|
Formatting = Newtonsoft.Json.Formatting.Indented,
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
|
PreserveReferencesHandling = PreserveReferencesHandling.None
|
|
};
|
|
|
|
//
|
|
return JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.None, setting);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert to Json With Support of Camel Casing
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <param name="camelCase"></param>
|
|
/// <returns></returns>
|
|
public static string ToJSON<T>(this T value, bool camelCase = false)
|
|
{
|
|
//
|
|
if (value.IsNull())
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
//
|
|
var setting = new JsonSerializerSettings
|
|
{
|
|
Formatting = Newtonsoft.Json.Formatting.Indented,
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
|
PreserveReferencesHandling = PreserveReferencesHandling.None,
|
|
};
|
|
|
|
//
|
|
if (camelCase)
|
|
{
|
|
setting.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
|
}
|
|
|
|
//
|
|
return JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.None, setting);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Map to Dynamic Object ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static dynamic ToDynamicObject(this string source)
|
|
{
|
|
return JsonConvert.DeserializeObject(source);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Map to Dynamic Object ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static dynamic ToDynamicObject(this object source)
|
|
{
|
|
//
|
|
var dMapConfig = new MapperConfiguration(b => { });
|
|
var dMapper = dMapConfig.CreateMapper();
|
|
|
|
//
|
|
return dMapper.Map<dynamic>(source);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parse From Dynamic Object ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static T FromDynamicObject<T>(this object source)
|
|
where T : class
|
|
{
|
|
//
|
|
var dMapConfig = new MapperConfiguration(b => { });
|
|
var dMapper = dMapConfig.CreateMapper();
|
|
dynamic dSource = source;
|
|
|
|
//
|
|
return dMapper.Map<T>(dSource);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert Type of Object to Safe Type
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static T ConvertTo<T>(this object source)
|
|
{
|
|
//
|
|
T result = default(T);
|
|
|
|
//
|
|
try
|
|
{
|
|
//
|
|
T item = (T)Convert.ChangeType(source, typeof(T));
|
|
|
|
//
|
|
result = item;
|
|
}
|
|
catch { }
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert an Instance to another Instance
|
|
/// using AutoMapper dynamic Convertion
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static T MapConvert<T>(this T source)
|
|
where T : class
|
|
{
|
|
//
|
|
if (source.IsNull())
|
|
{
|
|
return null;
|
|
}
|
|
|
|
//
|
|
var dSource = (object)source.ToDynamicObject();
|
|
return dSource.FromDynamicObject<T>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert Using Auto Mapper ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <typeparam name="E"></typeparam>
|
|
/// <returns></returns>
|
|
public static T MapConvert<T, E>(this E source)
|
|
where T : class
|
|
where E : class
|
|
{
|
|
//
|
|
if (source.IsNull())
|
|
{
|
|
return null;
|
|
}
|
|
|
|
//
|
|
var dMapConfig = new MapperConfiguration(b =>
|
|
{
|
|
b.CreateMap<T, E>()
|
|
.ReverseMap();
|
|
});
|
|
var dMapper = dMapConfig.CreateMapper();
|
|
|
|
//
|
|
var dSource = (object)dMapper.Map<dynamic>(source);
|
|
var result = dMapper.Map<T>(dSource);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert Using Auto Mapper ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <typeparam name="E"></typeparam>
|
|
/// <returns></returns>
|
|
public static IEnumerable<T> MapConvert<T, E>(this IEnumerable<E> source)
|
|
{
|
|
//
|
|
if (source.IsNull())
|
|
{
|
|
return null;
|
|
}
|
|
|
|
//
|
|
var dMapConfig = new MapperConfiguration(b =>
|
|
{
|
|
b.CreateMap<T, E>()
|
|
.ReverseMap();
|
|
});
|
|
var dMapper = dMapConfig.CreateMapper();
|
|
|
|
//
|
|
var dSource = (object)dMapper.Map<dynamic>(source);
|
|
var result = dMapper.Map<IEnumerable<T>>(dSource);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert Using Auto Mapper ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <typeparam name="E"></typeparam>
|
|
/// <returns></returns>
|
|
public static ICollection<T> MapConvert<T, E>(this ICollection<E> source)
|
|
{
|
|
//
|
|
if (source.IsNull())
|
|
{
|
|
return null;
|
|
}
|
|
|
|
//
|
|
var dMapConfig = new MapperConfiguration(b =>
|
|
{
|
|
b.CreateMap<T, E>()
|
|
.ReverseMap();
|
|
});
|
|
var dMapper = dMapConfig.CreateMapper();
|
|
|
|
//
|
|
var dSource = (object)dMapper.Map<dynamic>(source);
|
|
var result = dMapper.Map<ICollection<T>>(dSource);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert Using Auto Mapper ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <typeparam name="E"></typeparam>
|
|
/// <returns></returns>
|
|
public static IList<T> MapConvert<T, E>(this IList<E> source)
|
|
{
|
|
//
|
|
if (source.IsNull())
|
|
{
|
|
return null;
|
|
}
|
|
|
|
//
|
|
var dMapConfig = new MapperConfiguration(b =>
|
|
{
|
|
b.CreateMap<T, E>()
|
|
.ReverseMap();
|
|
});
|
|
var dMapper = dMapConfig.CreateMapper();
|
|
|
|
//
|
|
var dSource = (object)dMapper.Map<dynamic>(source);
|
|
var result = dMapper.Map<IList<T>>(dSource);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert Using Auto Mapper ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="TKey"></typeparam>
|
|
/// <typeparam name="TDest"></typeparam>
|
|
/// <typeparam name="TSource"></typeparam>
|
|
/// <returns></returns>
|
|
public static IDictionary<TKey, TDest> MapConvert<TKey, TDest, TSource>(this IDictionary<TKey, TSource> source)
|
|
{
|
|
//
|
|
if (source.IsNull())
|
|
{
|
|
return null;
|
|
}
|
|
|
|
//
|
|
var dMapConfig = new MapperConfiguration(b =>
|
|
{
|
|
b.CreateMap<TDest, TSource>()
|
|
.ReverseMap();
|
|
});
|
|
var dMapper = dMapConfig.CreateMapper();
|
|
|
|
//
|
|
var dSource = (object)dMapper.Map<dynamic>(source);
|
|
var result = dMapper.Map<IDictionary<TKey, TDest>>(dSource);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert a Type to Static Type
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static T ToStatic<T>(this object source)
|
|
{
|
|
//
|
|
var entity = Activator.CreateInstance<T>();
|
|
|
|
//
|
|
var properties = source as IDictionary<string, object>;
|
|
|
|
//
|
|
if (properties == null)
|
|
{
|
|
return entity;
|
|
}
|
|
|
|
//
|
|
foreach (var entry in properties)
|
|
{
|
|
//
|
|
var propertyInfo = entity.GetType().GetProperty(entry.Key);
|
|
if (propertyInfo != null)
|
|
{
|
|
propertyInfo.SetValue(entity, entry.Value, null);
|
|
}
|
|
}
|
|
|
|
//
|
|
return entity;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert From Json String ...
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static T FromJSON<T>(this string value)
|
|
{
|
|
return JsonConvert.DeserializeObject<T>(value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert From Json String ...
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <param name="type"></param>
|
|
/// <returns></returns>
|
|
public static object FromJSON(
|
|
this string value,
|
|
Type type
|
|
)
|
|
{
|
|
return JsonConvert
|
|
.DeserializeObject(value, type);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Search a Json String for Specific Node ...
|
|
/// </summary>
|
|
/// <param name="containerToken"></param>
|
|
/// <param name="name"></param>
|
|
/// <returns></returns>
|
|
public static List<JToken> FindTokens(this JToken containerToken, string name)
|
|
{
|
|
List<JToken> matches = new List<JToken>();
|
|
FindTokens(containerToken, name, matches);
|
|
return matches;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert to MD5 Bytes ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static byte[] ToMd5Bytes(this string source)
|
|
{
|
|
return new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(source));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert to MD5 String ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static string ToMd5String(this string source)
|
|
{
|
|
//
|
|
// string representation (similar to UNIX format)
|
|
return BitConverter.ToString(source.ToMd5Bytes())
|
|
// without dashes
|
|
.Replace("-", string.Empty)
|
|
// make lowercase
|
|
.ToLower();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check a String is MD5 String or not ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsMD5String(this string source)
|
|
{
|
|
//
|
|
if (String.IsNullOrEmpty(source))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
//
|
|
return Regex.IsMatch(source, "^[0-9a-fA-F]{32}$", RegexOptions.Compiled);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert an string to Byte array Representation ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static byte[] ToBytes(this string source)
|
|
{
|
|
return Encoding.UTF8.GetBytes(source);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts Byte Array to String Value ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static string FromBytes(this byte[] source)
|
|
{
|
|
//
|
|
var result = Encoding.UTF8.GetString(source, 0, source.Length);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check object is Null ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsNull(this object source)
|
|
{
|
|
return source == null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check collection Has Childs ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static bool HasChild<T>(this IList<T> source)
|
|
{
|
|
return !source.IsNull() && source.Count() > 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check collection Has Childs ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static bool HasChild<T>(this ICollection<T> source)
|
|
{
|
|
return !source.IsNull() && source.Count() > 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check collection Has Childs ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static bool HasChild<T>(this IEnumerable<T> source)
|
|
{
|
|
return !source.IsNull() && source.Count() > 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Select Async ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="method"></param>
|
|
/// <typeparam name="TSource"></typeparam>
|
|
/// <typeparam name="TResult"></typeparam>
|
|
/// <returns></returns>
|
|
public static async Task<IEnumerable<TResult>> SelectAsync<TSource, TResult>(
|
|
this IEnumerable<TSource> source, Func<TSource, Task<TResult>> method
|
|
)
|
|
{
|
|
return await Task.WhenAll(source.Select(async s => await method(s)));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts an Enumerable to Async Enumerable ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static async IAsyncEnumerable<T> ToAsyncEnumerable<T>(
|
|
this IEnumerable<T> source,
|
|
[System.Runtime.CompilerServices.EnumeratorCancellation]
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
foreach (var item in source)
|
|
{
|
|
//
|
|
if (cancellationToken.IsCancellationRequested)
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
//
|
|
yield return item;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts an Stream to Model AsyncEnumerator ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static async IAsyncEnumerable<T> ToModelAsyncEnumerable<T>(
|
|
this Stream source,
|
|
[System.Runtime.CompilerServices.EnumeratorCancellation]
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate Stream ...
|
|
if (source.IsNull())
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
//
|
|
while (source.CanRead)
|
|
{
|
|
//
|
|
var model = await source
|
|
.ReadModelFromStream<T>(cancellationToken);
|
|
|
|
//
|
|
yield return model;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts an Stream to String AsyncEnumerator ...
|
|
/// using Read Line of Stream Reader ...S
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public static async IAsyncEnumerable<string> ToStringAsyncEnumerable(
|
|
this Stream source,
|
|
[System.Runtime.CompilerServices.EnumeratorCancellation]
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate Stream ...
|
|
if (source.IsNull())
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
//
|
|
while (source.CanRead)
|
|
{
|
|
//
|
|
var json = await source
|
|
.ReadJsonFromStream(cancellationToken);
|
|
|
|
//
|
|
yield return json;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write a Model to Stream ...
|
|
/// </summary>
|
|
/// <param name="source">an Stream to Write ...</param>
|
|
/// <param name="model">a Model instance to Write ...</param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static async Task WriteModelToStream<T>(
|
|
this Stream source,
|
|
T model,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate Args ...
|
|
if (source.IsNull() || model.IsNull())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
// Extract Json of Model ...
|
|
var json = model.ToJSON(true);
|
|
|
|
//
|
|
await source.WriteJsonToStream(
|
|
json: json,
|
|
cancellationToken: cancellationToken
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write a Json String to Stream ...
|
|
/// </summary>
|
|
/// <param name="source">an Stream to Write ...</param>
|
|
/// <param name="json">json string to write ...</param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public static async Task WriteJsonToStream(
|
|
this Stream source,
|
|
string json,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
// Validate Args ...
|
|
if (source.IsNull() || json.IsNullOrEmpty())
|
|
{
|
|
return;
|
|
}
|
|
|
|
//
|
|
using (var streamWriter = new StreamWriter(stream: source))
|
|
{
|
|
//
|
|
using (var jsonWriter = new JsonTextWriter(streamWriter))
|
|
{
|
|
//
|
|
await jsonWriter.WriteRawAsync(json, cancellationToken);
|
|
await jsonWriter.FlushAsync(cancellationToken);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read a Json Model From Stream ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static async Task<T> ReadModelFromStream<T>(
|
|
this Stream source,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
T result = default(T);
|
|
|
|
//
|
|
var json = await source.ReadJsonFromStream(cancellationToken);
|
|
if (!json.IsNullOrEmpty())
|
|
{
|
|
//
|
|
try
|
|
{
|
|
result = json.FromJSON<T>();
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reading Json String From Stream ...
|
|
/// </summary>
|
|
/// <param name="source">an Stream to Write ...</param>
|
|
/// <param name="cancellationToken"></param>
|
|
/// <returns></returns>
|
|
public static async Task<string> ReadJsonFromStream(
|
|
this Stream source,
|
|
CancellationToken cancellationToken = default
|
|
)
|
|
{
|
|
//
|
|
var result = string.Empty;
|
|
|
|
//
|
|
// Validate Args ...
|
|
if (source.IsNull())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
using (var streamReader = new StreamReader(source))
|
|
{
|
|
//
|
|
result = await Task.Run(() =>
|
|
{
|
|
return streamReader.ReadLine();
|
|
},
|
|
cancellationToken);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a Collection item ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="sourceItem"></param>
|
|
/// <param name="updateWith"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static IList<T> Update<T>(
|
|
this IList<T> source,
|
|
T sourceItem,
|
|
T updateWith
|
|
)
|
|
{
|
|
//
|
|
var result = new List<T>();
|
|
|
|
//
|
|
// Validate Args ...
|
|
if (!source.HasChild() ||
|
|
updateWith.IsNull() ||
|
|
sourceItem.IsNull()
|
|
)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Extract Item from source ...
|
|
result = source
|
|
.Except(new List<T>() { sourceItem })
|
|
.ToList();
|
|
result.Add(updateWith);
|
|
|
|
//
|
|
return source;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a Collection item ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="sourceItem"></param>
|
|
/// <param name="updateWith"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static ICollection<T> Update<T>(
|
|
this ICollection<T> source,
|
|
T sourceItem,
|
|
T updateWith
|
|
)
|
|
{
|
|
//
|
|
var result = new List<T>();
|
|
|
|
//
|
|
// Validate Args ...
|
|
if (!source.HasChild() ||
|
|
updateWith.IsNull() ||
|
|
sourceItem.IsNull()
|
|
)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Extract Item from source ...
|
|
result = source
|
|
.Except(new List<T>() { sourceItem })
|
|
.ToList();
|
|
result.Add(updateWith);
|
|
|
|
//
|
|
return source;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update a Collection item ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="sourceItem"></param>
|
|
/// <param name="updateWith"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static IEnumerable<T> Update<T>(
|
|
this IEnumerable<T> source,
|
|
T sourceItem,
|
|
T updateWith
|
|
)
|
|
{
|
|
//
|
|
var result = new List<T>();
|
|
|
|
//
|
|
// Validate Args ...
|
|
if (!source.HasChild() ||
|
|
updateWith.IsNull() ||
|
|
sourceItem.IsNull()
|
|
)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Extract Item from source ...
|
|
result = source
|
|
.Except(new List<T>() { sourceItem })
|
|
.ToList();
|
|
result.Add(updateWith);
|
|
|
|
//
|
|
return source;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Select and Retrieve Nth Childs of given List ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="n"></param>
|
|
/// <param name="throwException"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static IEnumerable<T> GetNthItems<T>(this IList<T> source, int n, bool throwException = false)
|
|
{
|
|
//
|
|
var result = new List<T>();
|
|
if (n < 1 || n > source.Count || source.IsNull() || !source.HasChild())
|
|
{
|
|
if (throwException)
|
|
{
|
|
throw XException.InvalidArgs.ToException();
|
|
}
|
|
else
|
|
{
|
|
return result;
|
|
}
|
|
}
|
|
|
|
//
|
|
var index = 0;
|
|
foreach (var item in source)
|
|
{
|
|
//
|
|
result.Add(item);
|
|
index++;
|
|
|
|
//
|
|
if (index >= n)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Select and Retrieve Nth Childs of given List ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="n"></param>
|
|
/// <param name="throwException"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static IEnumerable<T> GetNthItems<T>(this ICollection<T> source, int n, bool throwException = false)
|
|
{
|
|
return source.ToList().GetNthItems(
|
|
n: n,
|
|
throwException: throwException
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Select and Retrieve Nth Childs of given List ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="n"></param>
|
|
/// <param name="throwException"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static IEnumerable<T> GetNthItems<T>(this IEnumerable<T> source, int n, bool throwException = false)
|
|
{
|
|
return source.ToList().GetNthItems(
|
|
n: n,
|
|
throwException: throwException
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check an string is Null or Empty ...
|
|
/// </summary>
|
|
/// <param name="sourse"></param>
|
|
/// <returns></returns>
|
|
public static bool IsNullOrEmpty(this string sourse)
|
|
{
|
|
return string.IsNullOrEmpty(sourse);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get string Value Attribute of an Enum ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static string GetStringValue(this Enum source)
|
|
{
|
|
var type = source.GetType();
|
|
var fieldInfo = type.GetField(source.ToString());
|
|
var attributes = fieldInfo.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];
|
|
|
|
return attributes != null && attributes.Length > 0 ?
|
|
attributes[0].StringValue : null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extracts digit's of an string ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static string GetDigits(this string source)
|
|
{
|
|
try
|
|
{
|
|
//
|
|
Regex regexObj = new Regex(@"[^\d]");
|
|
return regexObj.Replace(source, "");
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check an string is contains only digits ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsDigits(this string source)
|
|
{
|
|
return (source.GetDigits() != null &&
|
|
source.GetDigits().ToString() == source);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert to normal string, Trim and ToLower ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static string ToNormalString(this string source)
|
|
{
|
|
//
|
|
if (source.IsNullOrEmpty())
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
//
|
|
return source.Trim().ToLowerInvariant();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Capitalize an string ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static string Capitalize(this string source)
|
|
{
|
|
//
|
|
if (source.IsNullOrEmpty())
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
//
|
|
var result = "";
|
|
if (source.Length > 1)
|
|
{
|
|
//
|
|
var firstCharr = source.Substring(0, 1);
|
|
var otherParts = source.Substring(1, source.Length - 1);
|
|
|
|
//
|
|
result = firstCharr.ToUpperInvariant() + otherParts;
|
|
}
|
|
else
|
|
{
|
|
result = source.ToUpperInvariant();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Summarize a Text ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="maxAllowedLength"></param>
|
|
/// <param name="endsWith"></param>
|
|
/// <returns></returns>
|
|
public static string SummarizeContent(
|
|
this string source,
|
|
int maxAllowedLength = 0,
|
|
string endsWith = ""
|
|
)
|
|
{
|
|
//
|
|
var result = string.Empty;
|
|
if (source.IsNullOrEmpty() || maxAllowedLength <= 0)
|
|
{
|
|
//
|
|
result = source;
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Pattern of Links ...
|
|
var pattern = @"(?:[!]\[(.*?)\]\(.*?\))";
|
|
|
|
//
|
|
// Apply Summarization ...
|
|
|
|
//
|
|
// Replace Markdown Links using RegExp ...
|
|
result = Regex.Replace(source, pattern, "");
|
|
|
|
//
|
|
// Do Summarization if Length bigger than MaxLength ...
|
|
if (result.Length > maxAllowedLength)
|
|
{
|
|
result = result.Substring(0, maxAllowedLength);
|
|
}
|
|
|
|
//
|
|
// Apply EndsWith if Provided ...
|
|
if (!endsWith.IsNullOrEmpty() &&
|
|
!result.EndsWith(endsWith))
|
|
{
|
|
result += endsWith;
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check an string is a Valid Representation of PhoneNumber ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValidMobileNumber(this string source)
|
|
{
|
|
//
|
|
// Validating Mobile Numbers ...
|
|
var phoneNumberUtil = PhoneNumbers.PhoneNumberUtil.GetInstance();
|
|
try
|
|
{
|
|
//
|
|
var mPhoneNumber = phoneNumberUtil.Parse(source, null);
|
|
if (phoneNumberUtil.IsValidNumber(mPhoneNumber))
|
|
{
|
|
//
|
|
return true;
|
|
}
|
|
|
|
//
|
|
return false;
|
|
}
|
|
catch
|
|
{
|
|
|
|
//
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check an string is Valid Representation of Email Address ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValidEmail(this string source)
|
|
{
|
|
try
|
|
{
|
|
var addr = new System.Net.Mail.MailAddress(source);
|
|
return addr.Address == source;
|
|
}
|
|
catch
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check an string is Valid Representation of URL Address ...
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <returns></returns>
|
|
public static bool IsValidUrl(this string url)
|
|
{
|
|
return Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute);
|
|
}
|
|
|
|
/// <summary>
|
|
/// check a URL has / at the end, if not add one ...
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <returns></returns>
|
|
public static string NormalizeUrl(this string url)
|
|
{
|
|
return url.EndsWith("/") ? url : url + "/";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check an string is GUID or not ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsGuid(this string source)
|
|
{
|
|
//
|
|
// Validate Args ...
|
|
if (source.IsNullOrEmpty())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
//
|
|
Guid guid;
|
|
var result = Guid.TryParse(source, out guid);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Specific Guid string is Default Guid or not ...
|
|
/// </summary>
|
|
/// <param name="guid"></param>
|
|
/// <returns></returns>
|
|
public static bool IsDefaultGuid(this string guid)
|
|
{
|
|
//
|
|
if (!guid.IsGuid())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
//
|
|
var empty = Guid.Empty.ToString();
|
|
return guid == empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check Specific Guid is Default Guid or not ...
|
|
/// </summary>
|
|
/// <param name="guid"></param>
|
|
/// <returns></returns>
|
|
public static bool IsDefaultGuid(this Guid guid)
|
|
{
|
|
//
|
|
var empty = Guid.Empty;
|
|
return guid == empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parse an String to related Enumerable Items ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="separator"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static IEnumerable<T> ParseListString<T>(
|
|
this string source,
|
|
char? separator = null
|
|
)
|
|
{
|
|
//
|
|
if (!separator.HasValue)
|
|
{
|
|
separator = CommonConstants.DEFAULT_LIST_SEPERATOR;
|
|
}
|
|
|
|
//
|
|
var list = source.Split(separator.Value);
|
|
var result = new Collection<T>();
|
|
|
|
//
|
|
foreach (var s in list)
|
|
{
|
|
//
|
|
try
|
|
{
|
|
//
|
|
T item = (T)Convert.ChangeType(s, typeof(T));
|
|
|
|
//
|
|
result.Add(item);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a List String Representation to GUid List ...
|
|
/// </summary>
|
|
/// <param name="list"></param>
|
|
/// <param name="separator"></param>
|
|
/// <returns></returns>
|
|
public static IEnumerable<Guid> ParseListGuid(
|
|
this string list,
|
|
char? separator = null
|
|
)
|
|
{
|
|
//
|
|
var result = new List<Guid>();
|
|
|
|
//
|
|
if (!separator.HasValue)
|
|
{
|
|
separator = CommonConstants.DEFAULT_LIST_SEPERATOR;
|
|
}
|
|
|
|
//
|
|
if (list.IsNullOrEmpty())
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
var items = list.Split(separator.Value);
|
|
items.ToList().ForEach(i =>
|
|
{
|
|
//
|
|
if (!i.IsNullOrEmpty() && i.IsGuid() && !i.IsDefaultGuid())
|
|
{
|
|
//
|
|
var gID = new Guid(i);
|
|
result.Add(gID);
|
|
}
|
|
});
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert a List to List String ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="separator"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static string ToListString<T>(
|
|
this IEnumerable<T> source,
|
|
char? separator = null
|
|
)
|
|
{
|
|
//
|
|
if (!separator.HasValue)
|
|
{
|
|
separator = CommonConstants.DEFAULT_LIST_SEPERATOR;
|
|
}
|
|
|
|
//
|
|
if (!source.HasChild())
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
//
|
|
string result = String.Join(
|
|
separator
|
|
.ToString(),
|
|
source
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// get default value of a Type
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static T Default<T>(this T source)
|
|
where T : class
|
|
{
|
|
return default(T);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if an object is Null or Default Value
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <returns></returns>
|
|
public static bool IsNullOrDefault<T>(this T source)
|
|
where T : class
|
|
{
|
|
return Equals(source, default(T));
|
|
}
|
|
|
|
/// <summary>
|
|
/// convert a timestamp to ISO String representation ...
|
|
/// </summary>
|
|
/// <param name="timestamp"></param>
|
|
/// <returns></returns>
|
|
public static string ISODateString(this long timestamp)
|
|
{
|
|
//
|
|
var time = timestamp.FromTimestamp();
|
|
var result = time.ToString("o");
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Converts a DateTime to Unix Like Long ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static long ToTimestamp(
|
|
this DateTime source
|
|
)
|
|
{
|
|
return new DateTimeOffset(source)
|
|
.ToUnixTimeMilliseconds();
|
|
}
|
|
|
|
/// <summary>
|
|
/// convert timestamp to DateTime ...
|
|
/// </summary>
|
|
/// <param name="timestamp"></param>
|
|
/// <returns></returns>
|
|
public static DateTime FromTimestamp(this long timestamp)
|
|
{
|
|
//
|
|
var result = DateTimeOffset.FromUnixTimeMilliseconds(timestamp);
|
|
return result.DateTime;
|
|
}
|
|
public static DateTime FromTimestamp(this string source)
|
|
{
|
|
//
|
|
var timestamp = Int64.Parse(source);
|
|
var time = timestamp.FromTimestamp();
|
|
return time;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check a Date Time is Expired or not
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsExPired(this DateTime source)
|
|
{
|
|
//
|
|
if (source.IsNull())
|
|
{
|
|
return true;
|
|
}
|
|
|
|
//
|
|
var currentDate = DateTime.UtcNow;
|
|
var isExpired = currentDate >= source;
|
|
|
|
//
|
|
return isExpired;
|
|
}
|
|
public static bool IsExpired(this long source)
|
|
{
|
|
var time = source.FromTimestamp();
|
|
return time.IsExPired();
|
|
}
|
|
public static bool IsExpired(this string source)
|
|
{
|
|
//
|
|
var time = DateTime.Parse(source);
|
|
return time.IsExPired();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines a File Type is Image Kind or not
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static bool IsImageKind(this XFileType source)
|
|
{
|
|
//
|
|
var imageKind = new Collection<XFileType>() {
|
|
XFileType.Image,
|
|
XFileType.CoverImage,
|
|
XFileType.ProfileImasge
|
|
};
|
|
|
|
//
|
|
return imageKind.Contains(source);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Encode String as Url
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static string ToUrlEncoded(this string source)
|
|
{
|
|
return HttpUtility.UrlEncode(source);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decode Url String
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static string ToUrlDecoded(this string source)
|
|
{
|
|
return HttpUtility.UrlDecode(source);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Convert an Object to string for use in HttpParams
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <param name="cultureInfo"></param>
|
|
/// <returns></returns>
|
|
public static string ToHttpParamString(this object value, CultureInfo cultureInfo)
|
|
{
|
|
//
|
|
if (value is Enum)
|
|
{
|
|
//
|
|
string name = Enum.GetName(value.GetType(), value);
|
|
if (name != null)
|
|
{
|
|
//
|
|
var field = IntrospectionExtensions
|
|
.GetTypeInfo(value.GetType())
|
|
.GetDeclaredField(name);
|
|
|
|
//
|
|
if (field != null)
|
|
{
|
|
//
|
|
var attribute = CustomAttributeExtensions
|
|
.GetCustomAttribute(field, typeof(EnumMemberAttribute))
|
|
as EnumMemberAttribute;
|
|
|
|
//
|
|
if (attribute != null)
|
|
{
|
|
return attribute.Value != null ? attribute.Value : name;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (value is bool)
|
|
{
|
|
return System.Convert.ToString(value, cultureInfo).ToLowerInvariant();
|
|
}
|
|
else if (value is byte[])
|
|
{
|
|
return System.Convert.ToBase64String((byte[])value);
|
|
}
|
|
else if (value != null && value.GetType().IsArray)
|
|
{
|
|
//
|
|
var array = Enumerable.OfType<object>((Array)value);
|
|
return string.Join(",", Enumerable
|
|
.Select(
|
|
array,
|
|
o => o.ToHttpParamString(cultureInfo)));
|
|
}
|
|
|
|
//
|
|
return System.Convert.ToString(value, cultureInfo);
|
|
}
|
|
|
|
/// <summary>
|
|
/// ReShape an Object as HttpParam String
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public static string AsHttpParamString(this object value)
|
|
{
|
|
return System.Uri
|
|
.EscapeDataString(value
|
|
.ToHttpParamString(CultureInfo.InvariantCulture));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calculating Date Difference Provider String
|
|
/// and Generate Date based on UTC Time in Milliseconds like unix Date ...
|
|
/// </summary>
|
|
/// <param name="offsetProvider"></param>
|
|
/// <returns></returns>
|
|
public static long GenerateDateTimeOffset(this string offsetProvider)
|
|
{
|
|
//
|
|
if (offsetProvider.IsNullOrEmpty())
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
//
|
|
var mExpireValue = 0;
|
|
var mExpireValueStr = offsetProvider.GetDigits();
|
|
var mExpireUnit = offsetProvider.Replace(mExpireValueStr, "");
|
|
if (mExpireValueStr.IsNullOrEmpty() || mExpireUnit.IsNullOrEmpty())
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
//
|
|
var isParsed = int.TryParse(mExpireValueStr, out mExpireValue);
|
|
if (!isParsed)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
//
|
|
DateTime? date = null;
|
|
switch (mExpireUnit)
|
|
{
|
|
case "d":
|
|
date = DateTime.UtcNow.AddDays(mExpireValue);
|
|
break;
|
|
|
|
case "h":
|
|
date = DateTime.UtcNow.AddHours(mExpireValue);
|
|
break;
|
|
|
|
case "m":
|
|
date = DateTime.UtcNow.AddMinutes(mExpireValue);
|
|
break;
|
|
|
|
case "s":
|
|
date = DateTime.UtcNow.AddSeconds(mExpireValue);
|
|
break;
|
|
|
|
case "ms":
|
|
default:
|
|
date = DateTime.UtcNow.AddMilliseconds(mExpireValue);
|
|
break;
|
|
}
|
|
|
|
//
|
|
var result = !date.HasValue ? 0 : new DateTimeOffset(date.Value)
|
|
.ToUnixTimeMilliseconds();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
#region Int Id Extensions ...
|
|
public static bool IsValidIntId(this int source)
|
|
{
|
|
return source > 0;
|
|
}
|
|
|
|
public static void ValidateIntId(this int source)
|
|
{
|
|
//
|
|
if (!source.IsValidIntId())
|
|
{
|
|
XException.InvalidArgs.Throw();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region StringExtensions for Identity Server ...
|
|
public static string EnsureTrailingSlash(this string input)
|
|
{
|
|
if (!input.EndsWith("/"))
|
|
{
|
|
return input + "/";
|
|
}
|
|
|
|
return input;
|
|
}
|
|
|
|
public static bool IsMissing(this string value)
|
|
{
|
|
return string.IsNullOrWhiteSpace(value);
|
|
}
|
|
|
|
public static bool IsPresent(this string value)
|
|
{
|
|
return !string.IsNullOrWhiteSpace(value);
|
|
}
|
|
|
|
public static string Sha256(this string input)
|
|
{
|
|
if (input.IsMissing()) return string.Empty;
|
|
|
|
using (var sha = SHA256.Create())
|
|
{
|
|
var bytes = Encoding.UTF8.GetBytes(input);
|
|
var hash = sha.ComputeHash(bytes);
|
|
|
|
return Convert.ToBase64String(hash);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
//
|
|
#region Private ...
|
|
private static void FindTokens(JToken containerToken, string name, List<JToken> matches)
|
|
{
|
|
if (containerToken.Type == JTokenType.Object)
|
|
{
|
|
foreach (JProperty child in containerToken.Children<JProperty>())
|
|
{
|
|
if (child.Name == name)
|
|
{
|
|
matches.Add(child.Value);
|
|
}
|
|
FindTokens(child.Value, name, matches);
|
|
}
|
|
}
|
|
else if (containerToken.Type == JTokenType.Array)
|
|
{
|
|
foreach (JToken child in containerToken.Children())
|
|
{
|
|
FindTokens(child, name, matches);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
} |