public void addAttributeToProperty <AttrType>(PropertyInfo pi, AttrType attr = null) where AttrType : MysteryPropertyAttribute, new() { if (attr == null) { attr = new AttrType(); } Type generic_helper_type = typeof(MysteryPropertyAttributeHelper <,>); if (!_mystery_property_attributes_map.ContainsKey(pi)) { _mystery_property_attributes_map[pi] = new HashSet <MysteryPropertyAttribute>(); } attr.used_in = pi; Type this_property = generic_helper_type.MakeGenericType(pi.DeclaringType, pi.PropertyType); MysteryPropertyAttributeBaseHelper helper = (MysteryPropertyAttributeBaseHelper)Activator.CreateInstance(this_property, pi); attr.save = helper.save; attr.retrive = helper.retrive; attr.setup(); if (!_mystery_property_attributes.ContainsKey(pi.DeclaringType)) { _mystery_property_attributes[pi.DeclaringType] = new HashSet <MysteryPropertyAttribute>(); } //override declarations HashSet <MysteryPropertyAttribute> to_remove = new HashSet <MysteryPropertyAttribute>( from x in _mystery_property_attributes_map[pi] where (x) is AttrType select x ); foreach (AttrType att_to_remove in to_remove) { _mystery_property_attributes[pi.DeclaringType].Remove(att_to_remove); _mystery_property_attributes_map[pi].Remove(attr); } _mystery_property_attributes[pi.DeclaringType].Add(attr); _mystery_property_attributes_map[pi].Add(attr); }
public void registerType(Type type) { if (_registered_types.Contains(type)) { return; } _registered_types.Add(type); _types_by_name[type.FullName] = type; object[] CustomAttributes = type.GetCustomAttributes(false); _mystery_attributes[type] = new HashSet <MysteryClassAttribute>(); _mystery_attributes_dict[type] = new Dictionary <Type, MysteryClassAttribute>(); _mystery_property_attributes[type] = new HashSet <MysteryPropertyAttribute>(); //registering the type attributes foreach (Attribute cs in CustomAttributes) { Type at = cs.GetType(); if (!_attributes.ContainsKey(at)) { _attributes[at] = new HashSet <Type>(); } _attributes[at].Add(type); if ((cs) is MysteryClassAttribute) { MysteryClassAttribute ma = (MysteryClassAttribute)cs; ma.used_in = type; ma.setUp(); _mystery_attributes[type].Add(ma); _mystery_attributes_dict[type][ma.GetType()] = ma; } } Type generic_helper_type = typeof(MysteryPropertyAttributeHelper <,>); foreach (PropertyInfo pi in type.GetProperties()) { _mystery_property_attributes_map[pi] = new HashSet <MysteryPropertyAttribute>(); foreach (Attribute attr in pi.GetCustomAttributes(false)) { if (!(attr is MysteryPropertyAttribute)) { continue; } MysteryPropertyAttribute mi_attr = (MysteryPropertyAttribute)attr; mi_attr.used_in = pi; Type this_property = generic_helper_type.MakeGenericType(pi.DeclaringType, pi.PropertyType); MysteryPropertyAttributeBaseHelper helper = (MysteryPropertyAttributeBaseHelper)Activator.CreateInstance(this_property, pi); mi_attr.save = helper.save; mi_attr.retrive = helper.retrive; mi_attr.setup(); _mystery_property_attributes[type].Add(mi_attr); _mystery_property_attributes_map[pi].Add(mi_attr); } } //inheritance recording Type parent = type.BaseType; while (parent != null) { if (!_childs.ContainsKey(parent)) { _childs[parent] = new HashSet <Type>(); } _childs[parent].Add(type); parent = parent.BaseType; } foreach (Type implemented_interface in type.GetInterfaces()) { if (!_childs.ContainsKey(implemented_interface)) { _childs[implemented_interface] = new HashSet <Type>(); } _childs[implemented_interface].Add(type); } }