using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using Newtonsoft.Json;
using NPinyin;
namespace Common.Utils
{
public static class ConvertExtension
{
///
///
///
///
///
public static int ObjToInt(this object thisValue)
{
int reval = 0;
if (thisValue == null) return 0;
if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return reval;
}
public static bool ContainsAll(this IEnumerable source, IEnumerable items)
{
if (source == null || items == null)
return false;
var sourceCounts = source.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count());
var itemsCounts = items.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count());
foreach (var item in itemsCounts)
{
if (!sourceCounts.TryGetValue(item.Key, out int count) || count < item.Value)
{
return false;
}
}
return true;
}
public static IEnumerable RemoveRange(this IEnumerable source, IEnumerable items)
{
if (source == null)
return Enumerable.Empty();
if (items == null || !items.Any())
return source;
var list = new List(source);
foreach (var item in items)
{
int index = list.IndexOf(item);
if (index >= 0)
{
list.RemoveAt(index);
}
}
return list;
}
///
///
///
///
///
///
public static int ObjToInt(this object thisValue, int errorValue)
{
int reval = 0;
if (thisValue != null && thisValue != DBNull.Value && int.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
///
///
///
///
///
public static long ObjToLong(this object thisValue)
{
long reval = 0;
if (thisValue == null) return 0;
if (thisValue != null && thisValue != DBNull.Value && long.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return reval;
}
///
///
///
///
///
public static double ObjToMoney(this object thisValue)
{
double reval = 0;
if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return 0;
}
public static T GetRandomItem(this IEnumerable thisValue)
{
if (thisValue != null && thisValue.Count() > 0)
{
int index = Random.Shared.Next(thisValue.Count());
return thisValue.ToList()[index];
}
else if (thisValue.Count() == 1)
{
return thisValue.ToList()[0];
}
else
return default(T);
}
///
///
///
///
///
///
public static double ObjToMoney(this object thisValue, double errorValue)
{
double reval = 0;
if (thisValue != null && thisValue != DBNull.Value && double.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return errorValue;
}
///
///
///
///
///
public static string ObjToString(this object thisValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return "";
}
///
///
///
///
///
public static bool IsNotEmptyOrNull(this IEnumerable