public Dictionary <string, myPropInfo> Getproperties(Type type, string typename, bool IgnoreCaseOnDeserialize, bool customType) { Dictionary <string, myPropInfo> sd = null; if (_propertycache.TryGetValue(typename, out sd)) { return(sd); } else { sd = new Dictionary <string, myPropInfo>(); PropertyInfo[] pr = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo p in pr) { myPropInfo d = CreateMyProp(p.PropertyType, p.Name, customType); //if (p.CanWrite) // d.Flags |= myPropInfoFlags.CanWrite; d.setter = Reflection.CreateSetMethod(type, p); if (d.setter != null) { d.CanWrite = true;// Flags |= myPropInfoFlags.CanWrite; } d.getter = Reflection.CreateGetMethod(type, p); if (IgnoreCaseOnDeserialize) { sd.Add(p.Name.ToLower(), d); } else { sd.Add(p.Name, d); } } FieldInfo[] fi = type.GetFields(BindingFlags.Public | BindingFlags.Instance); foreach (FieldInfo f in fi) { myPropInfo d = CreateMyProp(f.FieldType, f.Name, customType); d.CanWrite = true;// Flags |= myPropInfoFlags.CanWrite; d.setter = Reflection.CreateSetField(type, f); d.getter = Reflection.CreateGetField(type, f); if (IgnoreCaseOnDeserialize) { sd.Add(f.Name.ToLower(), d); } else { sd.Add(f.Name, d); } } _propertycache.Add(typename, sd); return(sd); } }
public Dictionary <string, myPropInfo> Getproperties(Type type, string typename) { Dictionary <string, myPropInfo> sd = null; if (_propertycache.TryGetValue(typename, out sd)) { return(sd); } else { sd = new Dictionary <string, myPropInfo>(); var bf = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static; PropertyInfo[] pr = type.GetProperties(bf); foreach (PropertyInfo p in pr) { if (p.GetIndexParameters().Length > 0)// Property is an indexer { continue; } myPropInfo d = CreateMyProp(p.PropertyType, p.Name); d.setter = Reflection.CreateSetMethod(type, p); if (d.setter != null) { d.CanWrite = true; } d.getter = Reflection.CreateGetMethod(type, p); sd.Add(p.Name.ToLower(), d); } FieldInfo[] fi = type.GetFields(bf); foreach (FieldInfo f in fi) { myPropInfo d = CreateMyProp(f.FieldType, f.Name); if (f.IsLiteral == false) { d.setter = Reflection.CreateSetField(type, f); if (d.setter != null) { d.CanWrite = true; } d.getter = Reflection.CreateGetField(type, f); sd.Add(f.Name.ToLower(), d); } } _propertycache.Add(typename, sd); return(sd); } }
internal object ParseDictionary(Dictionary <string, object> d, Dictionary <string, object> globaltypes, Type type, object input) { if (type == typeof(Dictionary <string, object>)) { return(d); } object tn = ""; if (type == typeof(NameValueCollection)) { return(CreateNV(d)); } if (type == typeof(StringDictionary)) { return(CreateSD(d)); } if (d.TryGetValue("$i", out tn)) { object v = null; _cirrev.TryGetValue((int)(long)tn, out v); return(v); } if (d.TryGetValue("$types", out tn)) { _usingglobals = true; globaltypes = new Dictionary <string, object>(); foreach (var kv in (Dictionary <string, object>)tn) { globaltypes.Add((string)kv.Value, kv.Key); } } bool found = d.TryGetValue("$type", out tn); #if !SILVERLIGHT if (found == false && type == typeof(System.Object)) { return(d); // CreateDataset(d, globaltypes); } #endif if (found) { if (_usingglobals) { object tname = ""; if (globaltypes != null && globaltypes.TryGetValue((string)tn, out tname)) { tn = tname; } } type = Reflection.Instance.GetTypeFromCache((string)tn); } if (type == null) { throw new Exception("Cannot determine type"); } string typename = type.FullName; object o = input; if (o == null) { if (_params.ParametricConstructorOverride) { o = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(type); } else { o = Reflection.Instance.FastCreateInstance(type); } } int circount = 0; if (_circobj.TryGetValue(o, out circount) == false) { circount = _circobj.Count + 1; _circobj.Add(o, circount); _cirrev.Add(circount, o); } Dictionary <string, myPropInfo> props = Reflection.Instance.Getproperties(type, typename, Reflection.Instance.IsTypeRegistered(type)); foreach (var kv in d) { var n = kv.Key; var v = kv.Value; string name = n.ToLower(); if (name == "$map") { ProcessMap(o, props, (Dictionary <string, object>)d[name]); continue; } myPropInfo pi; // object doesn't contain property of specified name if (props.TryGetValue(name, out pi) == false) { // we need to look if it has alias field var aliasFieldProperty = type .GetProperties() .FirstOrDefault( property => Attribute.IsDefined(property, typeof(AliasField)) && property.GetCustomAttribute <AliasField>().Alias == name ); if (aliasFieldProperty == null) { continue; } // alias field is present - set it up so it can be processed pi = Reflection.Instance.CreateMyProp(aliasFieldProperty.PropertyType, aliasFieldProperty.Name, false); pi.setter = Reflection.CreateSetMethod(aliasFieldProperty.PropertyType, aliasFieldProperty); if (pi.setter != null) { pi.CanWrite = true; } pi.getter = Reflection.CreateGetMethod(aliasFieldProperty.PropertyType, aliasFieldProperty); } if (pi.CanWrite) { //object v = d[n]; if (v != null) { object oset = null; switch (pi.Type) { case myPropInfoType.Int: oset = (int)((long)v); break; case myPropInfoType.Long: oset = (long)v; break; case myPropInfoType.String: oset = (string)v; break; case myPropInfoType.Bool: oset = (bool)v; break; case myPropInfoType.DateTime: oset = CreateDateTime((string)v); break; case myPropInfoType.Enum: oset = CreateEnum(pi.pt, v); break; case myPropInfoType.Guid: oset = CreateGuid((string)v); break; case myPropInfoType.Array: if (!pi.IsValueType) { oset = CreateArray((List <object>)v, pi.pt, pi.bt, globaltypes); } // what about 'else'? break; case myPropInfoType.ByteArray: oset = Convert.FromBase64String((string)v); break; #if !SILVERLIGHT case myPropInfoType.DataSet: oset = CreateDataset((Dictionary <string, object>)v, globaltypes); break; case myPropInfoType.DataTable: oset = CreateDataTable((Dictionary <string, object>)v, globaltypes); break; case myPropInfoType.SortedList: oset = CreateSortedList((List <object>)v, pi, globaltypes); break; case myPropInfoType.Hashtable: // same case as Dictionary #endif case myPropInfoType.Dictionary: oset = CreateDictionary((List <object>)v, pi.pt, pi.GenericTypes, globaltypes); break; case myPropInfoType.StringKeyDictionary: oset = CreateStringKeyDictionary((Dictionary <string, object>)v, pi.pt, pi.GenericTypes, globaltypes); break; case myPropInfoType.NameValue: oset = CreateNV((Dictionary <string, object>)v); break; case myPropInfoType.StringDictionary: oset = CreateSD((Dictionary <string, object>)v); break; case myPropInfoType.Custom: oset = Reflection.Instance.CreateCustom((string)v, pi.pt); break; default: { if (pi.IsGenericType && pi.IsValueType == false && v is List <object> ) { oset = CreateGenericList((List <object>)v, pi.pt, pi.bt, globaltypes); } else if ((pi.IsClass || pi.IsStruct) && v is Dictionary <string, object> ) { oset = ParseDictionary((Dictionary <string, object>)v, globaltypes, pi.pt, pi.getter(o)); } else if (v is List <object> ) { oset = CreateArray((List <object>)v, pi.pt, typeof(object), globaltypes); } else if (pi.IsValueType) { oset = ChangeType(v, pi.changeType); } else { oset = v; } } break; } o = pi.setter(o, oset); } } } return(o); }