You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

270 lines
7.4 KiB

using System;
using System.Collections.Generic;
using System.Linq;
namespace LTEMvcApp.Models
{
/// <summary>
/// ASN1扩展方法
/// </summary>
public static class ASN1Extensions
{
/// <summary>
/// 安全获取字典值
/// </summary>
public static T GetValueOrDefault<T>(this Dictionary<string, object> dict, string key, T defaultValue = default(T))
{
if (dict != null && dict.ContainsKey(key))
{
try
{
return (T)Convert.ChangeType(dict[key], typeof(T));
}
catch
{
return defaultValue;
}
}
return defaultValue;
}
/// <summary>
/// 安全获取字典值
/// </summary>
public static object GetValueOrDefault(this Dictionary<string, object> dict, string key)
{
return dict?.ContainsKey(key) == true ? dict[key] : null;
}
/// <summary>
/// 将对象转换为字典
/// </summary>
public static Dictionary<string, object> ToDictionary(this object obj)
{
if (obj is Dictionary<string, object> dict)
return dict;
if (obj is IDictionary<string, object> idict)
return new Dictionary<string, object>(idict);
return null;
}
/// <summary>
/// 将对象转换为列表
/// </summary>
public static List<object> ToList(this object obj)
{
if (obj is List<object> list)
return list;
if (obj is IEnumerable<object> enumerable)
return enumerable.ToList();
return null;
}
/// <summary>
/// 安全转换为整数
/// </summary>
public static int? ToInt32(this object obj)
{
if (obj == null) return null;
try
{
return Convert.ToInt32(obj);
}
catch
{
return null;
}
}
/// <summary>
/// 安全转换为字符串
/// </summary>
public static string ToSafeString(this object obj)
{
return obj?.ToString();
}
/// <summary>
/// 安全转换为布尔值
/// </summary>
public static bool? ToBoolean(this object obj)
{
if (obj == null) return null;
try
{
return Convert.ToBoolean(obj);
}
catch
{
return null;
}
}
}
/// <summary>
/// ASN1工具类
/// </summary>
public static class ASN1Utils
{
/// <summary>
/// 创建ASN1对象
/// </summary>
public static Dictionary<string, object> CreateASN1Object()
{
return new Dictionary<string, object>();
}
/// <summary>
/// 创建ASN1数组
/// </summary>
public static List<object> CreateASN1Array()
{
return new List<object>();
}
/// <summary>
/// 添加属性到ASN1对象
/// </summary>
public static void AddProperty(Dictionary<string, object> obj, string name, object value)
{
if (obj == null) return;
if (string.IsNullOrEmpty(name))
{
if (obj.ContainsKey(""))
{
var existing = obj[""];
if (existing is List<object> list)
{
list.Add(value);
}
else
{
obj[""] = new List<object> { existing, value };
}
}
else
{
obj[""] = new List<object> { value };
}
}
else
{
if (obj.ContainsKey(name))
{
var existing = obj[name];
if (existing is List<object> list)
{
list.Add(value);
}
else
{
obj[name] = new List<object> { existing, value };
}
}
else
{
obj[name] = value;
}
}
}
/// <summary>
/// 添加元素到ASN1数组
/// </summary>
public static void AddElement(List<object> array, object value)
{
array?.Add(value);
}
/// <summary>
/// 深度克隆ASN1对象
/// </summary>
public static Dictionary<string, object> DeepClone(Dictionary<string, object> obj)
{
if (obj == null) return null;
var result = new Dictionary<string, object>();
foreach (var kvp in obj)
{
if (kvp.Value is Dictionary<string, object> dict)
{
result[kvp.Key] = DeepClone(dict);
}
else if (kvp.Value is List<object> list)
{
result[kvp.Key] = DeepCloneList(list);
}
else
{
result[kvp.Key] = kvp.Value;
}
}
return result;
}
/// <summary>
/// 深度克隆ASN1数组
/// </summary>
public static List<object> DeepCloneList(List<object> list)
{
if (list == null) return null;
var result = new List<object>();
foreach (var item in list)
{
if (item is Dictionary<string, object> dict)
{
result.Add(DeepClone(dict));
}
else if (item is List<object> subList)
{
result.Add(DeepCloneList(subList));
}
else
{
result.Add(item);
}
}
return result;
}
/// <summary>
/// 将ASN1对象转换为JSON字符串
/// </summary>
public static string ToJson(Dictionary<string, object> obj, bool prettyPrint = false)
{
if (obj == null) return "null";
var settings = new Newtonsoft.Json.JsonSerializerSettings
{
Formatting = prettyPrint ? Newtonsoft.Json.Formatting.Indented : Newtonsoft.Json.Formatting.None
};
return Newtonsoft.Json.JsonConvert.SerializeObject(obj, settings);
}
/// <summary>
/// 从JSON字符串创建ASN1对象
/// </summary>
public static Dictionary<string, object> FromJson(string json)
{
if (string.IsNullOrEmpty(json)) return null;
try
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
}
catch
{
return null;
}
}
}
}