private void Initialize(Type type) { PropertyInfo [] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo property in properties) { //if (!p.CanWrite) continue; object [] attributes = property.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false); if (attributes != null && attributes.Length > 0) { continue; } GenericGetter method = CreateGetMethod(property); if (method != null) { Add(new Getter { Name = property.Name, Method = method, PropertyType = property.PropertyType }); } } }
/// <summary> /// Конструктор CounterPropertyInfo /// </summary> /// <param name="containerType">Тип контейнера</param> /// <param name="counterProp">Свойство</param> /// <param name="counterAttrib">Атрибут</param> /// <param name="counterType">Тип счётчика</param> /// <param name="counterSetter">Делегат установки значения счётчика</param> /// <param name="couterGetter">Делегат для получения счётчика</param> public CounterPropertyInfo(Type containerType, PropertyInfo counterProp, CounterAttribute counterAttrib, CounterTypes counterType, GenericSetter counterSetter, GenericGetter couterGetter) { if (containerType == null) { throw new ArgumentNullException("containerType"); } if (counterProp == null) { throw new ArgumentNullException("counterProp"); } if (counterAttrib == null) { throw new ArgumentNullException("counterAttrib"); } if (counterSetter == null) { throw new ArgumentNullException("counterSetter"); } if (couterGetter == null) { throw new ArgumentNullException("couterGetter"); } CounterContainerType = containerType; Property = counterProp; Attribute = counterAttrib; CounterType = counterType; _setter = counterSetter; _getter = couterGetter; }
internal List <Getters> GetGetters(Type type) { if (_getterscache.ContainsKey(type)) { return(_getterscache[type]); } else { PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); List <Getters> getters = new List <Getters>(); foreach (PropertyInfo p in props) { GenericGetter g = CreateGetMethod(p); if (g != null) { Getters gg = new Getters(); gg.Name = p.Name; gg.Getter = g; getters.Add(gg); } } _getterscache.Add(type, getters); return(getters); } }
internal Getters[] GetGetters(Type type, bool showreadonly) { Getters[] val = null; if (_getterscache.TryGetValue(type, out val)) { return(val); } PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); List <Getters> getters = new List <Getters>(); foreach (PropertyInfo p in props) { if (!p.CanWrite && showreadonly == false) { continue; } #if !SILVERLIGHT && USE_XML object[] att = p.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false); if (att != null && att.Length > 0) { continue; } #endif GenericGetter g = CreateGetMethod(type, p); if (g != null) { Getters gg = new Getters(); gg.Name = p.Name; gg.Getter = g; //gg.propertyType = p.PropertyType; getters.Add(gg); } } FieldInfo[] fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static); foreach (var f in fi) { #if !SILVERLIGHT && USE_XML object[] att = f.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false); if (att != null && att.Length > 0) { continue; } #endif GenericGetter g = CreateGetField(type, f); if (g != null) { Getters gg = new Getters(); gg.Name = f.Name; gg.Getter = g; //gg.propertyType = f.FieldType; getters.Add(gg); } } val = getters.ToArray(); _getterscache.Add(type, val); return(val); }
internal List <Getters> GetGetters(Type type) { List <Getters> val = null; if (_getterscache.TryGetValue(type, out val)) { return(val); } PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); List <Getters> getters = new List <Getters>(); foreach (PropertyInfo p in props) { if (!p.CanWrite && ShowReadOnlyProperties == false) { continue; } object[] att = p.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false); if (att != null && att.Length > 0) { continue; } GenericGetter g = CreateGetMethod(type, p); if (g != null) { Getters gg = new Getters(); gg.Name = p.Name; gg.Getter = g; gg.propertyType = p.PropertyType; getters.Add(gg); } } FieldInfo[] fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (var f in fi) { object[] att = f.GetCustomAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute), false); if (att != null && att.Length > 0) { continue; } GenericGetter g = CreateGetField(type, f); if (g != null) { Getters gg = new Getters(); gg.Name = f.Name; gg.Getter = g; gg.propertyType = f.FieldType; getters.Add(gg); } } _getterscache.Add(type, getters); return(getters); }
public MemberCache(Type type, string name, MemberCache baseInfo) : this(baseInfo.MemberInfo, type, name) { Getter = baseInfo.Getter; Setter = baseInfo.Setter; IsProperty = baseInfo.IsProperty; IsStatic = baseInfo.IsStatic; IsReadOnly = baseInfo.IsReadOnly; }
public MemberCache(FieldInfo field) : this(field, field.FieldType, field.Name) { Getter = Reflection.CreateGetField(field); Setter = Reflection.CreateSetField(field); HasPublicGetter = HasPublicSetter = field.IsPublic; IsStatic = field.IsStatic; IsReadOnly = field.IsInitOnly; }
public MemberCache(PropertyInfo property) : this(property, property.PropertyType, property.Name) { Getter = Reflection.CreateGetProperty(property); Setter = Reflection.CreateSetProperty(property); HasPublicGetter = property.GetGetMethod() != null; HasPublicSetter = property.GetSetMethod() != null; IsProperty = true; IsStatic = (property.GetGetMethod(true) ?? property.GetSetMethod(true)).IsStatic; IsReadOnly = property.GetSetMethod() == null; // property.CanWrite can return true if the setter is non-public }
internal List <Getters> GetGetters(Type type) { List <Getters> val = null; if (_getterscache.TryGetValue(type, out val)) { return(val); } var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); var getters = new List <Getters>(); foreach (var p in props) { if (!p.CanWrite && ShowReadOnlyProperties == false) { continue; } var att = p.GetCustomAttributes(typeof(XmlIgnoreAttribute), false); if (att != null && att.Length > 0) { continue; } GenericGetter g = CreateGetMethod(p); if (g != null) { Getters gg = new Getters(); gg.Name = p.Name; gg.Getter = g; gg.propertyType = p.PropertyType; getters.Add(gg); } } _getterscache.Add(type, getters); return(getters); }
/// /// Creates a dynamic getter for the property /// public static GenericGetter CreateGetMethod(Type targetType, String propName) { GenericGetter result = null; PropertyInfo propertyInfo = targetType.GetProperty(propName, BindingFlags.NonPublic | BindingFlags.Instance); if (propertyInfo != null) { MethodInfo getMethod = propertyInfo.GetGetMethod(true); if (getMethod != null) { Type[] arguments = new Type[1]; arguments[0] = typeof(object); DynamicMethod getter = new DynamicMethod( String.Concat("_Get", propertyInfo.Name, "_"), typeof(object), arguments, propertyInfo.DeclaringType); ILGenerator generator = getter.GetILGenerator(); generator.DeclareLocal(typeof(object)); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Castclass, propertyInfo.DeclaringType); generator.EmitCall(OpCodes.Callvirt, getMethod, null); if (!propertyInfo.PropertyType.IsClass) { generator.Emit(OpCodes.Box, propertyInfo.PropertyType); } generator.Emit(OpCodes.Ret); result = (GenericGetter)getter.CreateDelegate(typeof(GenericGetter)); } } return(result); }
public Getters[] GetGetters(Type type, /*bool ShowReadOnlyProperties,*/ List <Type> IgnoreAttributes) { Getters[] val = null; if (_getterscache.TryGetValue(type, out val)) { return(val); } var bf = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static; //if (ShowReadOnlyProperties) // bf |= BindingFlags.NonPublic; PropertyInfo[] props = type.GetProperties(bf); List <Getters> getters = new List <Getters>(); foreach (PropertyInfo p in props) { bool read_only = false; if (p.GetIndexParameters().Length > 0) {// Property is an indexer continue; } if (!p.CanWrite) // && (ShowReadOnlyProperties == false))//|| isAnonymous == false)) { read_only = true; //continue; } if (IgnoreAttributes != null) { bool found = false; foreach (var ignoreAttr in IgnoreAttributes) { if (p.IsDefined(ignoreAttr, false)) { found = true; break; } } if (found) { continue; } } string mName = null; var att = p.GetCustomAttributes(true); foreach (var at in att) { #if NET4 if (at is System.Runtime.Serialization.DataMemberAttribute) { var dm = (System.Runtime.Serialization.DataMemberAttribute)at; if (dm.Name != "") { mName = dm.Name; } } #endif if (at is fastJSON.DataMemberAttribute) { var dm = (fastJSON.DataMemberAttribute)at; if (dm.Name != "") { mName = dm.Name; } } } GenericGetter g = (object obj) => { return(p.GetValue(obj)); }; if (g != null) { getters.Add(new Getters { Getter = g, Name = p.Name, lcName = p.Name.ToLowerInvariant(), memberName = mName, ReadOnly = read_only }); } } FieldInfo[] fi = type.GetFields(bf); foreach (var f in fi) { bool read_only = false; if (f.IsInitOnly) // && (ShowReadOnlyProperties == false))//|| isAnonymous == false)) { read_only = true; //continue; } if (IgnoreAttributes != null) { bool found = false; foreach (var ignoreAttr in IgnoreAttributes) { if (f.IsDefined(ignoreAttr, false)) { found = true; break; } } if (found) { continue; } } string mName = null; var att = f.GetCustomAttributes(true); foreach (var at in att) { #if NET4 if (at is System.Runtime.Serialization.DataMemberAttribute) { var dm = (System.Runtime.Serialization.DataMemberAttribute)at; if (dm.Name != "") { mName = dm.Name; } } #endif if (at is fastJSON.DataMemberAttribute) { var dm = (fastJSON.DataMemberAttribute)at; if (dm.Name != "") { mName = dm.Name; } } } if (f.IsLiteral == false) { GenericGetter g = CreateGetField(type, f); if (g != null) { getters.Add(new Getters { Getter = g, Name = f.Name, lcName = f.Name.ToLowerInvariant(), memberName = mName, ReadOnly = read_only }); } } } val = getters.ToArray(); _getterscache.Add(type, val); return(val); }
internal Getters[] GetGetters(Type type, bool ShowReadOnlyProperties, List <Type> IgnoreAttributes, bool UseFalseLiteral)//JSONParameters param) { Getters[] val = null; if (_getterscache.TryGetValue(type, out val)) { return(val); } PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); List <Getters> getters = new List <Getters>(); foreach (PropertyInfo p in props) { if (p.GetIndexParameters().Length > 0) {// Property is an indexer continue; } if (!p.CanWrite && ShowReadOnlyProperties == false) { continue; } if (IgnoreAttributes != null) { bool found = false; foreach (var ignoreAttr in IgnoreAttributes) { if (p.IsDefined(ignoreAttr, false)) { found = true; break; } } if (found) { continue; } } GenericGetter g = CreateGetMethod(type, p); #region 取自定义属性名 by itdos.com 2016-04-27 var tbl = p.DosGetCustomAttribute <JsonProp>(false) as JsonProp; var name = tbl != null ? tbl.PropertyName : p.Name; var lcName = name.ToLower(); #endregion if (g != null) { getters.Add(new Getters { Getter = g, Name = name, //p.Name, lcName = lcName //p.Name.ToLower() }); } } FieldInfo[] fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static); foreach (var f in fi) { if (IgnoreAttributes != null) { bool found = false; foreach (var ignoreAttr in IgnoreAttributes) { if (f.IsDefined(ignoreAttr, false)) { found = true; break; } } if (found) { continue; } } if (f.IsLiteral == false && UseFalseLiteral) { GenericGetter g = CreateGetField(type, f); if (g != null) { getters.Add(new Getters { Getter = g, Name = f.Name, lcName = f.Name.ToLower() }); } } } val = getters.ToArray(); _getterscache.Add(type, val); return(val); }
public Getters[] GetGetters(Type type, /*bool ShowReadOnlyProperties,*/ List <Type> IgnoreAttributes) { //修改了源码添加了针对部分属性进行序列化 bool jsonPart = type.GetCustomAttribute <JsonPartAttribute>() != default; Getters[] val = null; if (_getterscache.TryGetValue(type, out val)) { return(val); } var bf = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static; //if (ShowReadOnlyProperties) // bf |= BindingFlags.NonPublic; PropertyInfo[] props = type.GetProperties(bf); List <Getters> getters = new List <Getters>(); foreach (PropertyInfo property in props) { bool read_only = false; if (property.GetIndexParameters().Length > 0) {// Property is an indexer continue; } if (!property.CanWrite) // && (ShowReadOnlyProperties == false))//|| isAnonymous == false)) { read_only = true; //continue; } if (IgnoreAttributes != null) { bool found = false; foreach (var ignoreAttr in IgnoreAttributes) { if (property.IsDefined(ignoreAttr, false)) { found = true; break; } } if (found) { continue; } } string mName = null; var att = property.GetCustomAttributes(true); foreach (var at in att) { #if NET4 if (at is System.Runtime.Serialization.DataMemberAttribute) { var dm = (System.Runtime.Serialization.DataMemberAttribute)at; if (dm.Name != "") { mName = dm.Name; } } #endif if (at is FastJson.DataMemberAttribute) { var dm = (FastJson.DataMemberAttribute)at; if (dm.Name != "") { mName = dm.Name; } } } var jsonKeyAtt = property.GetCustomAttribute <JsonKeyAttribute>(); if (!jsonPart || (jsonKeyAtt != default && jsonPart)) { GenericGetter g = CreateGetMethod(type, property); if (g != null) { getters.Add(new Getters { Getter = g, Name = property.Name, lcName = property.Name.ToLowerInvariant(), memberName = mName, ReadOnly = read_only, keyName = jsonKeyAtt?.JsonKey }); } } } FieldInfo[] fields = type.GetFields(bf); foreach (var field in fields) { bool read_only = false; if (field.IsInitOnly) // && (ShowReadOnlyProperties == false))//|| isAnonymous == false)) { read_only = true; //continue; } if (IgnoreAttributes != null) { bool found = false; foreach (var ignoreAttr in IgnoreAttributes) { if (field.IsDefined(ignoreAttr, false)) { found = true; break; } } if (found) { continue; } } string mName = null; var att = field.GetCustomAttributes(true); foreach (var at in att) { #if NET4 if (at is System.Runtime.Serialization.DataMemberAttribute) { var dm = (System.Runtime.Serialization.DataMemberAttribute)at; if (dm.Name != "") { mName = dm.Name; } } #endif if (at is FastJson.DataMemberAttribute) { var dm = (FastJson.DataMemberAttribute)at; if (dm.Name != "") { mName = dm.Name; } } } if (field.IsLiteral == false) { var jsonKeyAtt = field.GetCustomAttribute <JsonKeyAttribute>(); if (!jsonPart || (jsonKeyAtt != default && jsonPart)) { GenericGetter g = CreateGetField(type, field); if (g != null) { getters.Add(new Getters { Getter = g, Name = field.Name, lcName = field.Name.ToLowerInvariant(), memberName = mName, ReadOnly = read_only, keyName = jsonKeyAtt?.JsonKey }); } } } } val = getters.ToArray(); _getterscache.Add(type, val); return(val); }
//internal static GenericSetter CreateSetField(Type type, FieldInfo fieldInfo) //{ // Type[] arguments = new Type[2]; // arguments[0] = arguments[1] = typeof(object); // DynamicMethod dynamicSet = new DynamicMethod("_", typeof(object), arguments, type); // ILGenerator il = dynamicSet.GetILGenerator(); // if (!type.IsClass) // structs // { // var lv = il.DeclareLocal(type); // il.Emit(OpCodes.Ldarg_0); // il.Emit(OpCodes.Unbox_Any, type); // il.Emit(OpCodes.Stloc_0); // il.Emit(OpCodes.Ldloca_S, lv); // il.Emit(OpCodes.Ldarg_1); // if (fieldInfo.FieldType.IsClass) // il.Emit(OpCodes.Castclass, fieldInfo.FieldType); // else // il.Emit(OpCodes.Unbox_Any, fieldInfo.FieldType); // il.Emit(OpCodes.Stfld, fieldInfo); // il.Emit(OpCodes.Ldloc_0); // il.Emit(OpCodes.Box, type); // il.Emit(OpCodes.Ret); // } // else // { // il.Emit(OpCodes.Ldarg_0); // il.Emit(OpCodes.Ldarg_1); // if (fieldInfo.FieldType.IsValueType) // il.Emit(OpCodes.Unbox_Any, fieldInfo.FieldType); // il.Emit(OpCodes.Stfld, fieldInfo); // il.Emit(OpCodes.Ldarg_0); // il.Emit(OpCodes.Ret); // } // return (GenericSetter)dynamicSet.CreateDelegate(typeof(GenericSetter)); //} //internal static GenericSetter CreateSetMethod(Type type, PropertyInfo propertyInfo) //{ // MethodInfo setMethod = propertyInfo.GetSetMethod(); // if (setMethod == null) // return null; // Type[] arguments = new Type[2]; // arguments[0] = arguments[1] = typeof(object); // DynamicMethod setter = new DynamicMethod("_", typeof(object), arguments); // ILGenerator il = setter.GetILGenerator(); // if (!type.IsClass) // structs // { // var lv = il.DeclareLocal(type); // il.Emit(OpCodes.Ldarg_0); // il.Emit(OpCodes.Unbox_Any, type); // il.Emit(OpCodes.Stloc_0); // il.Emit(OpCodes.Ldloca_S, lv); // il.Emit(OpCodes.Ldarg_1); // if (propertyInfo.PropertyType.IsClass) // il.Emit(OpCodes.Castclass, propertyInfo.PropertyType); // else // il.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType); // il.EmitCall(OpCodes.Call, setMethod, null); // il.Emit(OpCodes.Ldloc_0); // il.Emit(OpCodes.Box, type); // } // else // { // il.Emit(OpCodes.Ldarg_0); // il.Emit(OpCodes.Castclass, propertyInfo.DeclaringType); // il.Emit(OpCodes.Ldarg_1); // if (propertyInfo.PropertyType.IsClass) // il.Emit(OpCodes.Castclass, propertyInfo.PropertyType); // else // il.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType); // il.EmitCall(OpCodes.Callvirt, setMethod, null); // il.Emit(OpCodes.Ldarg_0); // } // il.Emit(OpCodes.Ret); // return (GenericSetter)setter.CreateDelegate(typeof(GenericSetter)); //} //internal static GenericGetter CreateGetField(Type type, FieldInfo fieldInfo) //{ // DynamicMethod dynamicGet = new DynamicMethod("_", typeof(object), new Type[] { typeof(object) }, type); // ILGenerator il = dynamicGet.GetILGenerator(); // if (!type.IsClass) // structs // { // var lv = il.DeclareLocal(type); // il.Emit(OpCodes.Ldarg_0); // il.Emit(OpCodes.Unbox_Any, type); // il.Emit(OpCodes.Stloc_0); // il.Emit(OpCodes.Ldloca_S, lv); // il.Emit(OpCodes.Ldfld, fieldInfo); // if (fieldInfo.FieldType.IsValueType) // il.Emit(OpCodes.Box, fieldInfo.FieldType); // } // else // { // il.Emit(OpCodes.Ldarg_0); // il.Emit(OpCodes.Ldfld, fieldInfo); // if (fieldInfo.FieldType.IsValueType) // il.Emit(OpCodes.Box, fieldInfo.FieldType); // } // il.Emit(OpCodes.Ret); // return (GenericGetter)dynamicGet.CreateDelegate(typeof(GenericGetter)); //} //internal static GenericGetter CreateGetMethod(Type type, PropertyInfo propertyInfo) //{ // MethodInfo getMethod = propertyInfo.GetGetMethod(); // if (getMethod == null) // return null; // DynamicMethod getter = new DynamicMethod("_", typeof(object), new Type[] { typeof(object) }, type); // ILGenerator il = getter.GetILGenerator(); // if (!type.IsClass) // structs // { // var lv = il.DeclareLocal(type); // il.Emit(OpCodes.Ldarg_0); // il.Emit(OpCodes.Unbox_Any, type); // il.Emit(OpCodes.Stloc_0); // il.Emit(OpCodes.Ldloca_S, lv); // il.EmitCall(OpCodes.Call, getMethod, null); // if (propertyInfo.PropertyType.IsValueType) // il.Emit(OpCodes.Box, propertyInfo.PropertyType); // } // else // { // il.Emit(OpCodes.Ldarg_0); // il.Emit(OpCodes.Castclass, propertyInfo.DeclaringType); // il.EmitCall(OpCodes.Callvirt, getMethod, null); // if (propertyInfo.PropertyType.IsValueType) // il.Emit(OpCodes.Box, propertyInfo.PropertyType); // } // il.Emit(OpCodes.Ret); // return (GenericGetter)getter.CreateDelegate(typeof(GenericGetter)); //} internal Getters[] GetGetters(Type type, JSONParameters param) { Getters[] val = null; if (_getterscache.TryGetValue(type, out val)) { return(val); } //PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); List <Getters> getters = new List <Getters>(); //foreach (PropertyInfo p in props) //{ // if (!p.CanWrite && param.ShowReadOnlyProperties == false) continue; // if (param.IgnoreAttributes != null) // { // bool found = false; // foreach (var ignoreAttr in param.IgnoreAttributes) // { // if (p.IsDefined(ignoreAttr, false)) // { // found = true; // break; // } // } // if (found) // continue; // } // GenericGetter g = CreateGetMethod(type, p); // if (g != null) // getters.Add(new Getters { Getter = g, Name = p.Name }); //} FieldInfo[] fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (var f in fi) { if (param.IgnoreAttributes != null) { bool found = false; foreach (var ignoreAttr in param.IgnoreAttributes) { if (f.IsDefined(ignoreAttr, false)) { found = true; break; } } if (found) { continue; } } GenericGetter g = f.GetValue;// CreateGetField(type, f); if (g != null) { getters.Add(new Getters { Getter = g, Name = f.Name }); } } val = getters.ToArray(); _getterscache.Add(type, val); return(val); }
public Getters[] GetGetters(Type type, bool ShowReadOnlyProperties, List <Type> IgnoreAttributes)// JSONParameters param) { Getters[] val = null; if (_getterscache.TryGetValue(type, out val)) { return(val); } PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static); List <Getters> getters = new List <Getters>(); foreach (PropertyInfo p in props) { if (p.GetIndexParameters().Length > 0) {// Property is an indexer continue; } if (!p.CanWrite && ShowReadOnlyProperties == false) { continue; } if (IgnoreAttributes != null) { bool found = false; foreach (var ignoreAttr in IgnoreAttributes) { if (p.IsDefined(ignoreAttr, false)) { found = true; break; } } if (found) { continue; } } GenericGetter g = CreateGetMethod(type, p); if (g != null) { getters.Add(new Getters { Getter = g, Name = p.Name, lcName = p.Name.ToLower() }); } } FieldInfo[] fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static); foreach (var f in fi) { if (IgnoreAttributes != null) { bool found = false; foreach (var ignoreAttr in IgnoreAttributes) { if (f.IsDefined(ignoreAttr, false)) { found = true; break; } } if (found) { continue; } } if (f.IsLiteral == false) { GenericGetter g = CreateGetField(type, f); if (g != null) { getters.Add(new Getters { Getter = g, Name = f.Name, lcName = f.Name.ToLower() }); } } } val = getters.ToArray(); _getterscache.Add(type, val); return(val); }
public Property(PropertyInfo info) { Info = info; Setter = CreateSetMethod(info); Getter = CreateGetMethod(info); }
public MemberCache(FieldInfo field) : this(field, field.FieldType, field.Name) { Getter = Reflection.CreateGetField (field); Setter = Reflection.CreateSetField (field); HasPublicGetter = HasPublicSetter = field.IsPublic; IsStatic = field.IsStatic; IsReadOnly = field.IsInitOnly; }
public MemberCache(PropertyInfo property) : this(property, property.PropertyType, property.Name) { Getter = Reflection.CreateGetProperty (property); Setter = Reflection.CreateSetProperty (property); HasPublicGetter = property.GetGetMethod () != null; HasPublicSetter = property.GetSetMethod () != null; IsProperty = true; IsStatic = (property.GetGetMethod (true) ?? property.GetSetMethod (true)).IsStatic; IsReadOnly = property.GetSetMethod () == null; // property.CanWrite can return true if the setter is non-public }
internal Getters[] GetGetters(Type type, BJSONParameters param) { Getters[] val = null; if (_getterscache.TryGetValue(type, out val)) { return(val); } PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); List <Getters> getters = new List <Getters>(); foreach (PropertyInfo p in props) { // CHANGED FOR ** LITEDB ** IgnoreProperty if (param.IgnoreProperty != null && p.MetadataToken == param.IgnoreProperty.MetadataToken && p.Module.Equals(param.IgnoreProperty.Module)) { continue; } if (!p.CanWrite && param.ShowReadOnlyProperties == false) { continue; } if (param.IgnoreAttributes != null) { bool found = false; foreach (var ignoreAttr in param.IgnoreAttributes) { if (p.IsDefined(ignoreAttr, false)) { found = true; break; } } if (found) { continue; } } GenericGetter g = CreateGetMethod(type, p); if (g != null) { getters.Add(new Getters { Getter = g, Name = p.Name }); } } FieldInfo[] fi = type.GetFields(BindingFlags.Instance | BindingFlags.Public); foreach (var f in fi) { if (param.IgnoreAttributes != null) { bool found = false; foreach (var ignoreAttr in param.IgnoreAttributes) { if (f.IsDefined(ignoreAttr, false)) { found = true; break; } } if (found) { continue; } } GenericGetter g = CreateGetField(type, f); if (g != null) { getters.Add(new Getters { Getter = g, Name = f.Name }); } } val = getters.ToArray(); _getterscache.Add(type, val); return(val); }
/// <summary> /// Use this method to override how your class can be, by default, mapped from entity to Bson document. /// Returns an EntityMapper from each requested Type /// </summary> protected virtual EntityMapper BuildEntityMapper(Type type) { var mapper = new EntityMapper { Members = new List <MemberMapper>(), ForType = type }; var idAttr = typeof(BsonIdAttribute); var ignoreAttr = typeof(BsonIgnoreAttribute); var fieldAttr = typeof(BsonFieldAttribute); var indexAttr = typeof(BsonIndexAttribute); var dbrefAttr = typeof(BsonRefAttribute); var members = this.GetTypeMembers(type); var id = this.GetIdMember(members); foreach (var memberInfo in members) { // checks [BsonIgnore] if (memberInfo.IsDefined(ignoreAttr, true)) { continue; } // checks field name conversion var name = this.ResolveFieldName(memberInfo.Name); // check if property has [BsonField] var field = (BsonFieldAttribute)memberInfo.GetCustomAttributes(fieldAttr, false).FirstOrDefault(); // check if property has [BsonField] with a custom field name if (field != null && field.Name != null) { name = field.Name; } // checks if memberInfo is id field if (memberInfo == id) { name = "_id"; } // test if field name is OK (avoid to check in all instances) - do not test internal classes, like DbRef if (BsonDocument.IsValidFieldName(name) == false) { throw LiteException.InvalidFormat(memberInfo.Name, name); } GenericGetter getter = null; GenericSetter setter = null; try { // create getter/setter function getter = Reflection.CreateGenericGetter(type, memberInfo); setter = Reflection.CreateGenericSetter(type, memberInfo); } catch { continue; //hh: added because some fields are not needed (unable to reflect), and the Custom Mapper is too late in this cycle to catch them. } // check if property has [BsonId] to get with was setted AutoId = true var autoId = (BsonIdAttribute)memberInfo.GetCustomAttributes(idAttr, false).FirstOrDefault(); // checks if this property has [BsonIndex] var index = (BsonIndexAttribute)memberInfo.GetCustomAttributes(indexAttr, false).FirstOrDefault(); // get data type var dataType = memberInfo is PropertyInfo ? (memberInfo as PropertyInfo).PropertyType : (memberInfo as FieldInfo).FieldType; // check if datatype is list/array var isList = Reflection.IsList(dataType); // create a property mapper var member = new MemberMapper { AutoId = autoId == null ? true : autoId.AutoId, FieldName = name, MemberName = memberInfo.Name, DataType = dataType, IsUnique = index == null ? false : index.Unique, IsList = isList, UnderlyingType = isList ? Reflection.GetListItemType(dataType) : dataType, Getter = getter, Setter = setter }; // check if property has [BsonRef] var dbRef = (BsonRefAttribute)memberInfo.GetCustomAttributes(dbrefAttr, false).FirstOrDefault(); if (dbRef != null && memberInfo is PropertyInfo) { BsonMapper.RegisterDbRef(this, member, dbRef.Collection ?? this.ResolveCollectionName((memberInfo as PropertyInfo).PropertyType)); } // support callback to user modify member mapper this.ResolveMember?.Invoke(type, memberInfo, member); //hh // test if has name and there is no duplicate field if (member.FieldName != null && mapper.Members.Any(x => x.FieldName == name) == false) { mapper.Members.Add(member); } } return(mapper); }
internal Getters[] GetGetters(Type type, bool ShowReadOnlyProperties, List <Type> IgnoreAttributes) { Getters[] val = null; if (_getterscache.TryGetValue(type, out val)) { return(val); } //bool isAnonymous = IsAnonymousType(type); var bf = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static; //if (ShowReadOnlyProperties) // bf |= BindingFlags.NonPublic; PropertyInfo[] props = type.GetProperties(bf); List <Getters> getters = new List <Getters>(); foreach (PropertyInfo p in props) { if (p.GetIndexParameters().Length > 0) {// Property is an indexer continue; } if (!p.CanWrite && (ShowReadOnlyProperties == false))//|| isAnonymous == false)) { continue; } if (IgnoreAttributes != null) { bool found = false; foreach (var ignoreAttr in IgnoreAttributes) { if (p.IsDefined(ignoreAttr, false)) { found = true; break; } } if (found) { continue; } } string mName = null; #if net4 var att = p.GetCustomAttributes(true); foreach (var at in att) { if (at is DataMemberAttribute) { var dm = (DataMemberAttribute)at; if (dm.Name != "") { mName = dm.Name; } } } #endif GenericGetter g = CreateGetMethod(type, p); if (g != null) { getters.Add(new Getters { Getter = g, Name = p.Name, lcName = p.Name.ToLower(), memberName = mName, member = p }); } } FieldInfo[] fi = type.GetFields(bf); foreach (var f in fi) { if (IgnoreAttributes != null) { bool found = false; foreach (var ignoreAttr in IgnoreAttributes) { if (f.IsDefined(ignoreAttr, false)) { found = true; break; } } if (found) { continue; } } string mName = null; #if net4 var att = f.GetCustomAttributes(true); foreach (var at in att) { if (at is DataMemberAttribute) { var dm = (DataMemberAttribute)at; if (dm.Name != "") { mName = dm.Name; } } } #endif if (f.IsLiteral == false) { GenericGetter g = CreateGetField(type, f); if (g != null) { getters.Add(new Getters { Getter = g, Name = f.Name, lcName = f.Name.ToLower(), memberName = mName, member = f }); } } } val = getters.ToArray(); _getterscache.Add(type, val); return(val); }
protected EntityMapper BuildEntityMapper(Type type, bool includeFields = false, bool includeNonPublic = false, bool ignoreSetter = false) { var mapper = new EntityMapper { Members = new List <MemberMapper>(), ForType = type }; MethodInfo[] listMethods = type.GetMethods(); mapper.Serializer = Reflection.MethodWithAttribute <Serialize>(listMethods); mapper.Deserializer = Reflection.MethodWithAttribute <Deserialize>(listMethods); if (mapper.Serializer != null && mapper.Deserializer != null) { return(mapper); } IEnumerable <MemberInfoWithMeta> members = null; bool isIndexed = IsTypeIndexed(type); if (isIndexed) { members = GetIndexedTypeMembers(type); } else { members = GetTypeMembers(type, includeFields, includeNonPublic); } foreach (MemberInfoWithMeta memberWithMeta in members) { MemberInfo memberInfo = memberWithMeta.Info; string name = memberInfo.Name; GenericGetter getter = null; GenericSetter setter = null; try { getter = Reflection.CreateGenericGetter(type, memberInfo); setter = Reflection.CreateGenericSetter(type, memberInfo); } catch (Exception ex) { throw new Exception("Could not generate getter and setter for type: " + type.ToString() + " member: " + name); } if (ignoreSetter == false) { if (getter == null || setter == null) { continue; //They're null when they don't exist } } Type dataType = memberInfo is PropertyInfo ? (memberInfo as PropertyInfo).PropertyType : (memberInfo as FieldInfo).FieldType; var member = new MemberMapper { Name = name, DataType = dataType, Getter = getter, Setter = setter, SkipIsNull = memberWithMeta.SkipIsNull, SkipType = memberWithMeta.SkipType }; mapper.Members.Add(member); } return(mapper); }