[3003] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Persistence.Core;
|
---|
| 6 | using System.Windows.Forms;
|
---|
| 7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 8 | using System.Reflection;
|
---|
| 9 |
|
---|
| 10 | namespace HeuristicLab.Persistence.GUI {
|
---|
| 11 | public class PersistenceAnalysis {
|
---|
| 12 |
|
---|
| 13 | public static bool IsSerializable(Type type, Configuration config) {
|
---|
| 14 | if (config.PrimitiveSerializers.Any(ps => ps.SourceType == type))
|
---|
| 15 | return true;
|
---|
| 16 | foreach (var cs in config.CompositeSerializers) {
|
---|
| 17 | if (cs.CanSerialize(type))
|
---|
| 18 | return true;
|
---|
| 19 | }
|
---|
| 20 | return false;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | private static bool DerivesFrom(Type baseType, Type type) {
|
---|
| 24 | if (type == baseType)
|
---|
| 25 | return true;
|
---|
| 26 | if (type.BaseType == null)
|
---|
| 27 | return false;
|
---|
| 28 | return DerivesFrom(baseType, type.BaseType);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | public static IEnumerable<Type> NonSerializableTypes(Configuration config) {
|
---|
| 32 | var types = new List<Type>();
|
---|
| 33 | var storableInconsistentcy = new List<Type>();
|
---|
| 34 | foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) {
|
---|
| 35 | if (assembly.FullName.StartsWith("System.") ||
|
---|
| 36 | assembly.FullName.StartsWith("HeuristicLab.PluginInfrastructure") ||
|
---|
| 37 | assembly.FullName.StartsWith("log4net") ||
|
---|
| 38 | assembly.FullName.StartsWith("WindowsBase") ||
|
---|
| 39 | assembly.FullName.StartsWith("WeifenLuo") ||
|
---|
| 40 | assembly.FullName.StartsWith("ICSharpCode") ||
|
---|
| 41 | assembly.FullName.StartsWith("Mono") ||
|
---|
| 42 | assembly.FullName.StartsWith("Netron"))
|
---|
| 43 | continue;
|
---|
| 44 | foreach (var type in assembly.GetTypes()) {
|
---|
| 45 | if (type.IsInterface || type.IsAbstract ||
|
---|
| 46 | type.FullName.StartsWith("System.") ||
|
---|
| 47 | type.FullName.StartsWith("Microsoft.") ||
|
---|
| 48 | type.FullName.Contains("<") ||
|
---|
| 49 | type.FullName.Contains(">") ||
|
---|
| 50 | DerivesFrom(typeof(Exception), type) ||
|
---|
| 51 | DerivesFrom(typeof(Control), type) ||
|
---|
| 52 | DerivesFrom(typeof(System.EventArgs), type) ||
|
---|
| 53 | DerivesFrom(typeof(System.Attribute), type) ||
|
---|
| 54 | type.GetInterface("HeuristicLab.MainForm.IUserInterfaceItem") != null
|
---|
| 55 | )
|
---|
| 56 | continue;
|
---|
| 57 | try {
|
---|
| 58 | if (!IsSerializable(type, config))
|
---|
| 59 | types.Add(type);
|
---|
[3027] | 60 | /* if (!IsCorrectlyStorable(type))
|
---|
| 61 | storableInconsistentcy.Add(type); */
|
---|
[3003] | 62 | } catch {
|
---|
| 63 | types.Add(type);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | return types;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[3027] | 70 | /* private static bool IsCorrectlyStorable(Type type) {
|
---|
[3003] | 71 | if (StorableAttribute.GetStorableMembers(type).Count() > 0) {
|
---|
| 72 | if (!StorableClassAttribute.IsStorableType(type, true))
|
---|
| 73 | return false;
|
---|
| 74 | if (type.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, Type.EmptyTypes, null) == null &&
|
---|
| 75 | StorableConstructorAttribute.GetStorableConstructor(type) == null)
|
---|
| 76 | return false;
|
---|
| 77 | }
|
---|
| 78 | return true;
|
---|
[3027] | 79 | } */
|
---|
[3003] | 80 | }
|
---|
| 81 | }
|
---|