public static object ToEntity(this IDictionary <string, object> source, Type type) { if (source.IsNullOrEmpty()) { return(null); } FastType fastType = FastType.Get(type); var instance = Activator.CreateInstance(type); foreach (var p in fastType.Setters) { if (p.Name.IsNullOrEmpty()) { continue; } if (source.Keys.Contains(p.Name)) { p.SetValue(instance, source[p.Name].ConvertToType(p.Type)); } } var temp = instance as IExtProperty; if (temp != null) { var keys = source.Keys.Where(o => o.StartsWith(SysConfig.ExtFieldName, StringComparison.OrdinalIgnoreCase)); if (!keys.IsNullOrEmpty()) { foreach (var key in keys) { temp.Properties[key] = source[key]; } } } return(instance); }
public static IDictionary <string, object> ToDictionary(this object value) { if (value == null) { return(null); } IDictionary <string, object> dictionary = null; if (typeof(IDictionary <string, object>).IsAssignableFrom(value.GetType())) { dictionary = value as IDictionary <string, object>; } else if (value.GetType().IsSubclassOf(typeof(NameValueCollection))) { var data = value as NameValueCollection; dictionary = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); foreach (var key in data.AllKeys) { dictionary.Add(key, data[key]); } } else { dictionary = new FoxOneDictionary <string, object>(StringComparer.OrdinalIgnoreCase); if (value is IExtProperty) { var prop = (value as IExtProperty).Properties; if (!prop.IsNullOrEmpty()) { dictionary.AddRange(prop); } } FastType fastType = FastType.Get(value.GetType()); fastType.Getters.ForEach(getter => { if (getter.Type.IsArray || getter.Type.IsValueType || getter.Type == typeof(string)) { string name = getter.Name; if (!name.IsNullOrEmpty()) { object val = getter.GetValue(value); dictionary.Add(name, val); } } }); } return(dictionary); }