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 PersistableProxyFactory(IPersistentDescriptor descriptor, Model model) { _Types = new Dictionary<Type, Type> (); _Descriptor = descriptor; _Model = model; }
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(); }
public static IMetaData[] FromAssembly(Assembly assembly, IPersistentDescriptor persDescriptor, params string[] namespaces) { List<IMetaData> metadata = new List<IMetaData>(); List<string> processed = new List<string>(); List<string> namespaceList; if (namespaces != null) namespaceList = new List<string>(namespaces); else namespaceList = new List<string>(0); foreach (Type type in assembly.GetExportedTypes()) { if (namespaceList.Count > 0 && !namespaceList.Contains(type.Namespace)) continue; if (!type.IsPublic) { continue; } // Ignore none business types if (typeof(ICollection).IsAssignableFrom(type)) { continue; } // Ignore types from System assembly if (type.Assembly == typeof(String).Assembly) { continue; } #if !EUSS11 // Ignores generic types declared globally as they can't be persisted. Only use those which are used as relationships (really used) // i.e: Gen<T> is ignored, but not Gen<string> if (type.IsGenericType) { continue; } #endif string typeName = TypeResolver.ConvertNamespaceDomainToEuss(type); if (type.IsInterface) { metadata.Add(new InterfaceMetadata(typeName)); } if (processed.Contains(typeName) || (type.Attributes & TypeAttributes.Sealed) == TypeAttributes.Sealed) continue; object[] attributes = type.GetCustomAttributes(typeof(NotSerializedAttribute), true); if (attributes.Length > 0 && attributes[0] is NotSerializedAttribute) continue; ImportType(persDescriptor, metadata, processed, type, typeName); } return metadata.ToArray(); }
public static IMetaData[] FromAssembly(string assemblyName, IPersistentDescriptor persDescriptor) { if (assemblyName == null) throw new ArgumentNullException("assemblyName"); string[] assemblyNameAndNamespaces = new string[] { assemblyName }; if (assemblyName.Contains(":")) assemblyNameAndNamespaces = assemblyName.Split(':'); assemblyName = assemblyNameAndNamespaces[assemblyNameAndNamespaces.Length - 1]; string[] namespaces = new string[assemblyNameAndNamespaces.Length - 1]; for (int i = 0; i < assemblyNameAndNamespaces.Length - 1; i++) namespaces[i] = assemblyNameAndNamespaces[i]; return FromAssembly(Assembly.Load(assemblyName), persDescriptor, namespaces); }