1282 lines
39 KiB
C#
1282 lines
39 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.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 {
|
|
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 {
|
|
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>
|
|
/// 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>
|
|
/// 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>
|
|
/// 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>
|
|
/// 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>
|
|
/// 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;
|
|
}
|
|
|
|
/// <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 == null) {
|
|
return true;
|
|
}
|
|
|
|
//
|
|
var currentDate = DateTime.UtcNow;
|
|
var isExpired = currentDate >= source;
|
|
|
|
//
|
|
return 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
|
|
}
|
|
} |