/// <summary> /// Cascade a null flavor /// </summary> static void CascadeNullFlavor(IImplementsNullFlavor me, NullFlavor flavor) { me.NullFlavor = flavor; foreach (var propertyInfo in me.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { Type propertyType = propertyInfo.PropertyType; if (typeof(IList).IsAssignableFrom(propertyType)) // correct for lists propertyType = propertyType.GetGenericArguments()[0]; // Only IImpleentsNullFlavor should be allowed if (!typeof(IImplementsNullFlavor).IsAssignableFrom(propertyType)) continue; var instance = propertyInfo.GetValue(me, null) as IImplementsNullFlavor; // Make some intelligent decisions about populating var propertyAtt = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true); if (propertyAtt.Length > 0) { var strongPropertyAtt = propertyAtt[0] as PropertyAttribute; // Don't set a null flavor on structural (attributes) if (strongPropertyAtt.PropertyType == PropertyAttribute.AttributeAttributeType.Structural) continue; if (strongPropertyAtt.Conformance == PropertyAttribute.AttributeConformanceType.Optional && instance == null) continue; } // is this a list or IImplementsNullFlavor if (instance == null) { var constructorInfo = propertyType.GetConstructor(Type.EmptyTypes); if (constructorInfo == null) continue; // sanity check, can't create // Construct an instance instance = constructorInfo.Invoke(null) as IImplementsNullFlavor; } // Don't cascade down data types if (instance is IAny) instance.NullFlavor = flavor; else CascadeNullFlavor(instance, flavor); // Set if not a list if (propertyInfo.PropertyType == propertyType) propertyInfo.SetValue(me, instance, null); else // Add to list if not (propertyInfo.GetValue(me, null) as IList).Add(instance); } }
/// <summary> /// Cascade a null flavor /// </summary> static void CascadeNullFlavor(IImplementsNullFlavor me, NullFlavor flavor) { me.NullFlavor = flavor; foreach (var propertyInfo in me.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { Type propertyType = propertyInfo.PropertyType; if (typeof(IList).IsAssignableFrom(propertyType)) // correct for lists { propertyType = propertyType.GetGenericArguments()[0]; } // Only IImpleentsNullFlavor should be allowed if (!typeof(IImplementsNullFlavor).IsAssignableFrom(propertyType)) { continue; } var instance = propertyInfo.GetValue(me, null) as IImplementsNullFlavor; // Make some intelligent decisions about populating var propertyAtt = propertyInfo.GetCustomAttributes(typeof(PropertyAttribute), true); if (propertyAtt.Length > 0) { var strongPropertyAtt = propertyAtt[0] as PropertyAttribute; // Don't set a null flavor on structural (attributes) if (strongPropertyAtt.PropertyType == PropertyAttribute.AttributeAttributeType.Structural) { continue; } if (strongPropertyAtt.Conformance == PropertyAttribute.AttributeConformanceType.Optional && instance == null) { continue; } } // is this a list or IImplementsNullFlavor if (instance == null) { var constructorInfo = propertyType.GetConstructor(Type.EmptyTypes); if (constructorInfo == null) { continue; // sanity check, can't create } // Construct an instance instance = constructorInfo.Invoke(null) as IImplementsNullFlavor; } // Don't cascade down data types if (instance is IAny) { instance.NullFlavor = flavor; } else { CascadeNullFlavor(instance, flavor); } // Set if not a list if (propertyInfo.PropertyType == propertyType) { propertyInfo.SetValue(me, instance, null); } else // Add to list if not { (propertyInfo.GetValue(me, null) as IList).Add(instance); } } }