/// <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; }
public void SetSearchResultField(string fieldName, object instance, object value) { Type t = instance.GetType(); if (!_cache.ContainsKey(t)) { AddTypeToCache(t); } SearchContentFieldInfo field = _cache[t].Find( delegate(SearchContentFieldInfo fi) { return(fi.Name == fieldName); }); if (field.Name != fieldName) { throw new Exception(String.Format("Field with name \"{0}\" not found on type \"{1}\"!", fieldName, t)); } GenericSetter setter = CreateSetMethod(field.PropertyInfo); if (setter == null) { throw new Exception(String.Format("Property \"{0}\" does not have setter!", field.PropertyInfo.Name)); } setter(instance, Convert.ChangeType(value, field.PropertyInfo.PropertyType)); }
private void surfaceDessin_SetSelectionText(object sender, RoutedEventArgs e) { if (surfaceDessin.GetSelectedStrokes().Count > 1) { MessageBox.Show("Error : Too many elements selected"); } else if (surfaceDessin.GetSelectedStrokes().Count == 0) { MessageBox.Show("Error : No element selected"); } else if ((surfaceDessin.GetSelectedStrokes()[0] as Form).Type == "UmlClass") { string name = (surfaceDessin.GetSelectedStrokes()[0] as UMLClass).Label; string border = (surfaceDessin.GetSelectedStrokes()[0] as UMLClass).BorderStyle; List <string> methods = (surfaceDessin.GetSelectedStrokes()[0] as UMLClass).Methods; List <string> attributes = (surfaceDessin.GetSelectedStrokes()[0] as UMLClass).Attributes; UmlClassSetter menu = new UmlClassSetter(name, border, methods, attributes); surfaceDessin.Visibility = Visibility.Hidden; menu.Show(); menu.Closing += new CancelEventHandler(UMLSetterClosingHandler); } else { string label = (surfaceDessin.GetSelectedStrokes()[0] as Form).Label; string border = (surfaceDessin.GetSelectedStrokes()[0] as Form).BorderStyle; GenericSetter menu = new GenericSetter(label, border); surfaceDessin.Visibility = Visibility.Hidden; menu.Show(); menu.Closing += new CancelEventHandler(GenericSetterClosingHandler); } }
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 }
private GenericSetter GetSetter(PropertyInfo prop) { if (_settercache.ContainsKey(prop)) { return(_settercache[prop]); } else { GenericSetter s = CreateSetMethod(prop); _settercache.Add(prop, s); return(s); } }
/// /// Creates a dynamic setter for the property /// public static GenericSetter CreateSetMethod(Type targetType, String propName) { GenericSetter result = null; PropertyInfo propertyInfo = targetType.GetProperty(propName, BindingFlags.NonPublic | BindingFlags.Instance); if (propertyInfo != null) { MethodInfo setMethod = propertyInfo.GetSetMethod(true); if (setMethod != null) { Type[] arguments = new Type[2]; arguments[0] = arguments[1] = typeof(object); DynamicMethod setter = new DynamicMethod( String.Concat("_Set", propertyInfo.Name, "_"), typeof(void), arguments, propertyInfo.DeclaringType); ILGenerator generator = setter.GetILGenerator(); generator.Emit(OpCodes.Ldarg_0); generator.Emit(OpCodes.Castclass, propertyInfo.DeclaringType); generator.Emit(OpCodes.Ldarg_1); if (propertyInfo.PropertyType.IsClass) { generator.Emit(OpCodes.Castclass, propertyInfo.PropertyType); } else { generator.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType); } generator.EmitCall(OpCodes.Callvirt, setMethod, null); generator.Emit(OpCodes.Ret); result = (GenericSetter)setter.CreateDelegate(typeof(GenericSetter)); } } return(result); }
public Property(PropertyInfo info) { Info = info; Setter = CreateSetMethod(info); Getter = CreateGetMethod(info); }
/// <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); }
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 }
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); }