/// <summary> /// Indicates whether the property value should be added to the final XElement representation /// based on the attribute and the value of the property /// </summary> /// <param name="beerAttribute"></param> /// <param name="propertyValue"></param> /// <returns></returns> private bool ShouldAddProperty(BeerXMLIncludeAttribute beerAttribute, object propertyValue) { if (beerAttribute == null) { return(false); } bool propertyIsNull = propertyValue == null; if (beerAttribute.Requirement == PropertyRequirement.REQUIRED) { if (propertyIsNull) { throw new InvalidOperationException("Required type with null value"); } return(true); } if (propertyIsNull) { return(false); } return(true); }
/// <summary> /// Ensures the property mapping dictionaries are built. /// </summary> private static void EnsurePropertyMappings() { // dictionaries are built, just return if (_isInit) { return; } // get the assembly Assembly assembly = Assembly.GetExecutingAssembly(); // find every IBeerXMLEntity type in the assembly that is a non-abstract class foreach (Type type in assembly.GetTypes().Where(t => typeof(IBeerXMLEntity).IsAssignableFrom(t) && t.IsClass && !t.IsAbstract)) { // add the name -> type mapping _typeNameToTypeMap.Add(type.Name, type); IDictionary <string, BeerXMLProperty> propInfoList = new Dictionary <string, BeerXMLProperty>(StringComparer.OrdinalIgnoreCase); // find every property that has the BeerXMLIncludeAttribute on it foreach (PropertyInfo property in type.GetProperties()) { BeerXMLIncludeAttribute attribute = property.GetCustomAttribute(typeof(BeerXMLIncludeAttribute), inherit: true) as BeerXMLIncludeAttribute; if (attribute != null) { BeerXMLProperty prop = new BeerXMLProperty(property, attribute, typeof(IBeerXMLEntity).IsAssignableFrom(property.PropertyType)); propInfoList.Add(prop.Name, prop); } } // add to dictionary _typeToPropertyMap.Add(type, propInfoList); } // don't run this again _isInit = true; }
/// <summary> /// Initializes a new instance of the <see cref="BeerXMLProperty"/> class. /// </summary> /// <param name="property">The property.</param> /// <param name="attribute">The attribute.</param> /// <param name="isIBeerXMLEntity">if set to <c>true</c> [is i beer XML entity].</param> public BeerXMLProperty(PropertyInfo property, BeerXMLIncludeAttribute attribute, bool isIBeerXMLEntity) { this.Property = property; this.Attribute = attribute; this.IsIBeerXMLEntity = isIBeerXMLEntity; }