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