public static bool ShouldGenerateMember(PropertyInfo propertyInfo, JsGeneratorOptions generatorOptions) { if (!generatorOptions.RespectDataMemberAttribute) { return(true); } var customAttributes = propertyInfo.GetCustomAttributes(true); return(customAttributes.All(p => (p as IgnoreDataMemberAttribute) == null)); }
public static bool HasDefaultValue(PropertyInfo propertyInfo, JsGeneratorOptions generatorOptions) { if (!generatorOptions.RespectDefaultValueAttribute) { return(false); } var customAttributes = propertyInfo.GetCustomAttributes(true); if (customAttributes.All(p => (p as DefaultValueAttribute) == null)) { return(false); } return(true); }
public static string GetPropertyName(PropertyInfo propertyInfo, JsGeneratorOptions generatorOptions) { if (!generatorOptions.RespectDataMemberAttribute) { return(propertyInfo.Name); } var customAttributes = propertyInfo.GetCustomAttributes(true); if (customAttributes.All(p => (p as DataMemberAttribute) == null)) { return(propertyInfo.Name); } var dataMemberAttribute = (DataMemberAttribute)customAttributes.First(p => (p as DataMemberAttribute) != null); return(!string.IsNullOrWhiteSpace(dataMemberAttribute.Name) ? dataMemberAttribute.Name : propertyInfo.Name); }
public static IEnumerable <PropertyBag> GetPropertyDictionaryForTypeGeneration( IEnumerable <Type> types, JsGeneratorOptions generatorOptions, List <PropertyBag> propertyTypeCollection = null) { if (propertyTypeCollection == null) { propertyTypeCollection = new List <PropertyBag>(); } foreach (Type type in types) { if (type.IsEnum) { string[] getVals = type.GetEnumNames(); string typeName = type.Name; foreach (string enumVal in getVals) { if (generatorOptions.TreatEnumsAsStrings) { propertyTypeCollection.Add(new PropertyBag(typeName, type, enumVal, typeof(string), null, PropertyBag.TransformablePropertyTypeEnum.Primitive, true, enumVal)); } else { object trueVal = Convert.ChangeType(Enum.Parse(type, enumVal), type.GetEnumUnderlyingType()); propertyTypeCollection.Add(new PropertyBag(typeName, type, enumVal, type.GetEnumUnderlyingType(), null, PropertyBag.TransformablePropertyTypeEnum.Primitive, true, trueVal)); } } } else { PropertyInfo[] props = type.GetProperties(); string typeName = type.Name; foreach (PropertyInfo prop in props) { if (!Helpers.ShouldGenerateMember(prop, generatorOptions)) { continue; } string propertyName = Helpers.GetPropertyName(prop, generatorOptions); Type propertyType = prop.PropertyType; if (!Helpers.IsPrimitive(propertyType)) { if (Helpers.IsCollectionType(propertyType)) { List <PropertyBagTypeInfo> collectionInnerTypes = GetCollectionInnerTypes(propertyType); bool isDictionaryType = Helpers.IsDictionaryType(propertyType); propertyTypeCollection.Add(new PropertyBag(typeName, type, propertyName, propertyType, collectionInnerTypes, isDictionaryType ? PropertyBag.TransformablePropertyTypeEnum.DictionaryType : PropertyBag.TransformablePropertyTypeEnum.CollectionType, false, null)); //if primitive, no need to reflect type if (collectionInnerTypes.All(p => p.IsPrimitiveType)) { continue; } foreach (PropertyBagTypeInfo collectionInnerType in collectionInnerTypes.Where(p => !p.IsPrimitiveType)) { string innerTypeName = collectionInnerType.Type.Name; if (propertyTypeCollection.All(p => p.TypeName != innerTypeName)) { GetPropertyDictionaryForTypeGeneration(new[] { collectionInnerType.Type }, generatorOptions, propertyTypeCollection); } } } else { propertyTypeCollection.Add(new PropertyBag(typeName, type, propertyName, propertyType, null, PropertyBag.TransformablePropertyTypeEnum.ReferenceType, false, null)); if (propertyTypeCollection.All(p => p.TypeName != propertyType.Name)) { GetPropertyDictionaryForTypeGeneration(new[] { propertyType }, generatorOptions, propertyTypeCollection); } } } else { bool hasDefaultValue = Helpers.HasDefaultValue(prop, generatorOptions); if (hasDefaultValue) { object val = Helpers.ReadDefaultValueFromAttribute(prop); propertyTypeCollection.Add(new PropertyBag(typeName, type, propertyName, propertyType, null, PropertyBag.TransformablePropertyTypeEnum.Primitive, true, val)); } else { propertyTypeCollection.Add(new PropertyBag(typeName, type, propertyName, propertyType, null, PropertyBag.TransformablePropertyTypeEnum.Primitive, false, null)); } if (propertyType.IsEnum) { if (propertyTypeCollection.All(p => p.TypeName != propertyType.Name)) { GetPropertyDictionaryForTypeGeneration(new[] { propertyType }, generatorOptions, propertyTypeCollection); } } } } } } return(propertyTypeCollection); }