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