private static void ImportType(IPersistentDescriptor persDescriptor, IList<IMetaData> metadata, ICollection<string> processed, Type type, string typeName) { if (processed.Contains(typeName) || type == typeof(object)) { return; } processed.Add(typeName); PropertyDescriptor[] properties = persDescriptor.GetPersistentProperties(type); string parentTypeName = type.BaseType == typeof(object) || type.IsInterface ? String.Empty : TypeResolver.ConvertNamespaceDomainToEuss(type.BaseType); metadata.Add(new TypeMetaData(typeName, parentTypeName, false)); foreach (Type intf in type.GetInterfaces()) { metadata.Add(new TypeMetaData(typeName, TypeResolver.ConvertNamespaceDomainToEuss(intf), true)); } foreach (PropertyDescriptor descriptor in properties) { // Relation if (descriptor.IsEntity) { Type propType = descriptor.Type; if (descriptor.IsList) { // Get the child type when the reference is a collection PersistentPropertyAttribute[] attrs = type.GetProperty(descriptor.PropertyName).GetCustomAttributes( typeof(PersistentPropertyAttribute), false) as PersistentPropertyAttribute[]; if (attrs != null && attrs.Length > 0) { PersistentPropertyAttribute ppa = attrs[0]; propType = ppa.Type; } } #if !EUSS11 if (descriptor.IsGenericList) { propType = DescriptorHelper.GetGenericType(descriptor.Type); } bool isList = descriptor.IsList || descriptor.IsGenericList; #else bool isList = descriptor.IsList; #endif // Ignores relationships which are not IList if (descriptor.IsList && descriptor.Type != typeof(IList)) continue; // Ignores relationships to undefined Generic types (a relationship to Generic<T>, when the class is itsel generic) if (propType.FullName == null) continue; // Ignore types from System assembly if (propType.Assembly == typeof(String).Assembly) { continue; } // Ignore none business types if (propType.IsPublic && !typeof(IEnumerable).IsAssignableFrom(propType)) { // Ignore types from System assembly if (propType.Assembly != typeof(String).Assembly) { metadata.Add(new ReferenceMetaData(typeName, descriptor.PropertyName, TypeResolver.ConvertNamespaceDomainToEuss(propType), descriptor.IsComposition, !descriptor.IsComposition, isList)); } ImportType(persDescriptor, metadata, processed, propType, TypeResolver.ConvertNamespaceDomainToEuss(propType)); } } else { if (descriptor.Type.IsEnum) { Type enumType = typeof(string); // Try to get an attribute for this property which defines the type of the enum (i.e.: int or string) PropertyInfo pi = type.GetProperty(descriptor.PropertyName, descriptor.Type); if (pi != null) { PersistentPropertyAttribute[] attrs = pi.GetCustomAttributes( typeof(PersistentPropertyAttribute), false) as PersistentPropertyAttribute[]; // Add this property if more than one Attribute if (attrs != null && attrs.Length > 0) { // Takes the first attribute even if more are set PersistentPropertyAttribute ppa = attrs[0]; if (ppa.Type != null) enumType = ppa.Type; } } List<string> values = new List<string>(); foreach (object val in EnumHelper.GetValues(descriptor.Type)) values.Add(val.ToString()); PropertyMetaData pm = new PropertyMetaData(typeName, descriptor.PropertyName, enumType, false); pm.Values = values.ToArray(); metadata.Add(pm); } else metadata.Add(new PropertyMetaData(typeName, descriptor.PropertyName, descriptor.Type, false)); } } }
public static IMetaData[] FromPropertyDescriptor(Type type, IPersistentDescriptor persDescriptor, bool derivedTypes) { List<IMetaData> metadata = new List<IMetaData>(); string typeName = TypeResolver.ConvertNamespaceDomainToEuss(type); metadata.Add(new TypeMetaData(typeName)); foreach (PropertyDescriptor descriptor in persDescriptor.GetPersistentProperties(type)) { // Relation if (descriptor.IsEntity) { Type propType = descriptor.Type; if (descriptor.IsList) { // Get the child type when the reference is a collection PersistentPropertyAttribute[] attrs = type.GetProperty(descriptor.PropertyName).GetCustomAttributes( typeof(PersistentPropertyAttribute), false) as PersistentPropertyAttribute[]; if (attrs != null && attrs.Length > 0) { PersistentPropertyAttribute ppa = attrs[0]; propType = ppa.Type; } } if (descriptor.IsGenericList) { propType = DescriptorHelper.GetGenericType(descriptor.Type); } bool isList = descriptor.IsList || descriptor.IsGenericList; metadata.Add(new ReferenceMetaData(typeName, descriptor.PropertyName, TypeResolver.ConvertNamespaceDomainToEuss(propType), descriptor.IsComposition, !descriptor.IsComposition, isList)); } else { if (descriptor.Type.IsEnum) { Type enumType = typeof(string); // Try to get an attribute for this property which defines the type of the enum (i.e.: int or string) PropertyInfo pi = type.GetProperty(descriptor.PropertyName, descriptor.Type); if (pi != null) { PersistentPropertyAttribute[] attrs = pi.GetCustomAttributes( typeof(PersistentPropertyAttribute), false) as PersistentPropertyAttribute[]; // Add this property if more than one Attribute if (attrs != null && attrs.Length > 0) { // Takes the first attribute even if more are set PersistentPropertyAttribute ppa = attrs[0]; if (ppa.Type != null) enumType = ppa.Type; } } List<string> values = new List<string>(); foreach (object val in EnumHelper.GetValues(descriptor.Type)) values.Add(val.ToString()); PropertyMetaData pm = new PropertyMetaData(typeName, descriptor.PropertyName, enumType, false); pm.Values = values.ToArray(); metadata.Add(pm); } else metadata.Add(new PropertyMetaData(typeName, descriptor.PropertyName, descriptor.Type, false)); } } if (derivedTypes) foreach (Type dt in type.Assembly.GetTypes()) if (dt.BaseType == type) { metadata.AddRange(FromPropertyDescriptor(dt, persDescriptor, true)); metadata.Add(new TypeMetaData(TypeResolver.ConvertNamespaceDomainToEuss(dt), TypeResolver.ConvertNamespaceDomainToEuss(type), type.IsInterface)); } return metadata.ToArray(); }