using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace BarrageGrab
{
///
/// 扩展类
///
public static class ObjectExtension
{
///
/// 判断字符串是否为空或空白
///
public static bool IsNullOrWhiteSpace(this string str) => string.IsNullOrWhiteSpace(str);
///
/// 判断字符串是否为空
///
public static bool IsNullOrEmpty(this string str) => string.IsNullOrEmpty(str);
///
/// 转为Json
///
///
///
public static string ToJson(this object obj,bool format = false)
{
if (obj is string)
{
return obj.ToString();
}
return JsonConvert.SerializeObject(obj, format?Formatting.Indented:Formatting.None);
}
///
/// 转为对象
///
///
///
///
public static T AsObject(this string json)
{
return JsonConvert.DeserializeObject(json);
}
///
/// 获取Value或其Value默认值
///
public static TValue ValueOrDefault(this IDictionary dic, Tkey key)
{
if (dic.ContainsKey(key))
{
return dic[key];
}
return default(TValue);
}
///
/// 获取Value或其Value默认值
///
public static TValue ValueOrDefault(this IDictionary dic, Tkey key, TValue defaultValue)
{
if (dic.ContainsKey(key))
{
return dic[key];
}
return defaultValue;
}
///
/// 遍历集合
///
public static void Foreach(this IEnumerable list, Action func)
{
foreach (var item in list) func(item);
}
///
/// 遍历集合
///
public static void Foreach(this IEnumerable list, Action func)
{
int i = 0;
foreach (var item in list) func(item, i++);
}
///
/// 是否包含
///
///
///
///
public static bool In(this T obj, params T[] args)
{
return args.Contains(obj);
}
///
/// 是否包含
///
///
///
///
public static bool LikeIn(this string str, params string[] args)
{
return args.Any(a => str.Contains(a));
}
public static CookieCollection ToCookies(this string cookieStr, string domain)
{
var cookies = new CookieCollection();
if (!string.IsNullOrEmpty(cookieStr))
{
var cookieArray = cookieStr.Split(';');
foreach (var cookie in cookieArray)
{
var cookieParts = cookie.Split('=');
if (cookieParts.Length == 2)
{
var cookieName = cookieParts[0].Trim();
var cookieValue = cookieParts[1].Trim();
var newCookie = new Cookie(cookieName, Uri.EscapeDataString(cookieValue));
newCookie.Path = "/";
newCookie.Domain = domain;
cookies.Add(newCookie);
}
}
}
return cookies;
}
///
/// 追加另一个字典
///
///
///
///
public static IDictionary Append(this IEnumerable> dic, IEnumerable> other)
{
var res = new Dictionary();
foreach (var item in dic)
{
res.Add(item.Key, item.Value);
}
foreach (var item in other)
{
if (res.ContainsKey(item.Key))
{
res[item.Key] = item.Value;
}
else
{
res.Add(item.Key, item.Value);
}
}
return res;
}
///
/// 转换为Cookie对象列表
///
///
///
///
public static CookieCollection ToCookies(this IDictionary dic, string domain)
{
var cookieCollection = new CookieCollection();
foreach (var item in dic)
{
cookieCollection.Add(new Cookie(item.Key, item.Value, "/", domain));
}
return cookieCollection;
}
///
/// 添加Cookie
///
///
///
///
public static void PushCookies(this CookieContainer cookies, string domain, IDictionary dic)
{
if (cookies == null) return;
var cookieCollection = dic.ToCookies(domain);
cookies.Add(cookieCollection);
}
}
}