private bool IsSerializable(KeyValuePair <string, object> variable)
        {
            Type type         = null;
            bool serializable = false;

            if (variable.Value != null)
            {
                type = variable.Value.GetType();
                if (serializableLookup.TryGetValue(type, out serializable))
                {
                    return(serializable);
                }
                if (StorableTypeAttribute.IsStorableType(type))
                {
                    return(serializableLookup[type] = true);
                }
            }

            var ser = new ProtoBufSerializer();

            try {
                using (var memStream = new MemoryStream()) {
                    ser.Serialize(variable.Value, memStream); // try to serialize to memory stream
                    serializable = true;
                }
            } catch (PersistenceException) {
                serializable = false;
            }

            if (type != null)
            {
                serializableLookup[type] = serializable;
            }
            return(serializable);
        }
Exemple #2
0
        public static void ReplaceTypeImplementation(Type old, Guid oldGuid, Type @new)
        {
            DeregisterType(oldGuid);
            DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(@new).Guid);

            RegisterType(oldGuid, @new);
            SetTypeGuid(@new, oldGuid);
            typeInfos.Remove(old);
        }
        public void DuplicateGuidTest()
        {
            var guid = StorableTypeAttribute.GetStorableTypeAttribute(typeof(Type_2F344E0B)).Guid;

            try {
                TypeManipulation.RegisterType(guid, typeof(TypeWithDuplicateGuid));
                Assert.Fail("This method should not succeed.");
            } catch (PersistenceException e) {
                Assert.AreEqual($"PersistenceException in type HEAL.Attic.Tests.ExceptionTests+{nameof(TypeWithDuplicateGuid)}:" +
                                $" The GUID {guid} is already used by type HEAL.Attic.Tests.ExceptionTests+{nameof(Type_2F344E0B)}.", e.Message);
                Assert.AreEqual(TypeManipulation.GetType(guid), typeof(Type_2F344E0B));
            }
        }
Exemple #4
0
 private static StorableTypeAttribute GetStorableTypeAttribute(Type type)
 {
     lock (storableTypeCache) {
         if (storableTypeCache.ContainsKey(type))
         {
             return(storableTypeCache[type]);
         }
         StorableTypeAttribute attribute = type
                                           .GetCustomAttributes(typeof(StorableTypeAttribute), false)
                                           .SingleOrDefault() as StorableTypeAttribute;
         storableTypeCache.Add(type, attribute);
         return(attribute);
     }
 }
Exemple #5
0
        public void CheckDuplicateGUIDs()
        {
            // easy to produce duplicate GUIDs with copy&paste
            var dict       = new Dictionary <Guid, string>();
            var duplicates = new Dictionary <string, string>();

            // using AppDomain instead of ApplicationManager so that NonDiscoverableTypes are also checked
            foreach (Type type in AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()))
            {
                var attr = StorableTypeAttribute.GetStorableTypeAttribute(type);
                if (attr == null)
                {
                    continue;
                }

                foreach (var guid in attr.Guids)
                {
                    if (!dict.ContainsKey(guid))
                    {
                        dict.Add(guid, type.FullName);
                    }
                    else
                    {
                        duplicates.Add(type.FullName, dict[guid]);
                    }
                }
            }

            foreach (var kvp in duplicates)
            {
                Console.WriteLine($"{kvp.Key} has same GUID as {kvp.Value}");
            }

            if (duplicates.Any())
            {
                Assert.Fail("Duplicate GUIDs found.");
            }
        }
Exemple #6
0
        public void TestStorableClass()
        {
            var errorMessage = new StringBuilder();

            foreach (var type in ApplicationManager.Manager.GetTypes(typeof(object), onlyInstantiable: false, includeGenericTypeDefinitions: true)
                     .Where(t => t.Namespace != null && !t.Namespace.Contains(".Tests"))
                     .Where(t => !StorableTypeAttribute.IsStorableType(t)))
            {
                var members             = type.GetMembers(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly);
                var storableConstructor = members.SingleOrDefault(m => Attribute.IsDefined(m, typeof(StorableConstructorAttribute), inherit: false));
                var storableMembers     = members.Where(m => Attribute.IsDefined(m, typeof(StorableAttribute), inherit: false));

                if (storableConstructor != null)
                {
                    errorMessage.Append(Environment.NewLine + type.Namespace + "." + type.GetPrettyName() + ": Contains a storable constructor but is not a storable type.");
                }
                else if (storableMembers.Any())
                {
                    errorMessage.Append(Environment.NewLine + type.Namespace + "." + type.GetPrettyName() + ": Contains at least one storable member but is not a storable type.");
                }
            }
            Assert.IsTrue(errorMessage.Length == 0, errorMessage.ToString());
        }