示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
 private static void GetCompositeProperty(NameValueCollection source, object instance, FastProperty p, bool needKh, string prefix = "")
 {
     if (p.Type.IsGenericType)
     {
         var pType = p.Type.GetGenericArguments()[0];
         var t     = typeof(List <>);
         t = t.MakeGenericType(pType);
         var    listInstance = Activator.CreateInstance(t);
         int    i            = 0;
         string keyTemplate  = needKh ? "{0}[{1}][{2}]" : "{0}{1}[{2}]";
         while (source.AllKeys.Any(o => o.StartsWith(keyTemplate.FormatTo(prefix, p.Name, i))))
         {
             string key       = keyTemplate.FormatTo(prefix, p.Name, i);
             object pInstance = null;
             if (pType.IsInterface)
             {
                 pInstance = ObjectHelper.GetObject(pType);
             }
             else
             {
                 pInstance = Activator.CreateInstance(pType);
             }
             var pInstanceType = FastType.Get(pInstance.GetType());
             foreach (var pp in pInstanceType.Setters)
             {
                 if (pp.Type == typeof(string) || pp.Type.IsValueType)
                 {
                     string tempKey = "{0}[{1}]".FormatTo(key, pp.Name);
                     if (source.AllKeys.Contains(tempKey))
                     {
                         pp.SetValue(pInstance, source[tempKey].ConvertToType(pp.Type));
                     }
                 }
                 else
                 {
                     GetCompositeProperty(source, pInstance, pp, true, key);
                 }
             }
             var add = listInstance.GetType().GetMethod("Add");
             add.Invoke(listInstance, new object[] { pInstance });
             i++;
         }
         p.SetValue(instance, listInstance.ConvertToType(p.Type));
     }
 }
示例#4
0
        public static T ToEntity <T>(this NameValueCollection source) where T : class, new()
        {
            var      type     = typeof(T);
            FastType fastType = FastType.Get(type);
            var      instance = Activator.CreateInstance(type);

            foreach (var p in fastType.Setters)
            {
                if (p.Name.IsNullOrEmpty())
                {
                    continue;
                }
                if (source.AllKeys.Contains(p.Name))
                {
                    p.SetValue(instance, source[p.Name].ConvertToType(p.Type));
                }
                GetCompositeProperty(source, instance, p, false);
            }
            return(instance as T);
        }
示例#5
0
        private static PropertyMapping[] GetSetterMappings(Type type, IDataReader reader, string mappingKey = null)
        {
            PropertyMapping[]      mappings   = null;
            FastType               reflection = FastType.Get(type);
            List <PropertyMapping> list       = new List <PropertyMapping>();

            for (int i = 0; i < reader.FieldCount; i++)
            {
                string       columnName = reader.GetName(i);
                FastProperty prop       = reflection.Setters.SingleOrDefault(m => MatchColumnName(m.Name, columnName));
                if (prop != null)
                {
                    list.Add(new PropertyMapping()
                    {
                        Prop = prop, Index = i
                    });
                }
            }
            mappings = list.ToArray();
            return(mappings);
        }