Esempio n. 1
0
        private void GetSerialisables(Assembly assembly, string dir, DasherAssemblyInfo result, int depth = 0)
        {
            if (depth > 2)
            {
                return;            //just in case of circular references. Too large number slows down the whole operation
            }
            foreach (var t in assembly.GetTypes())
            {
                var attributes = t.GetCustomAttributes(typeof(DasherSerialisableAttribute), false).Cast <DasherSerialisableAttribute>().ToList();
                if (attributes.Any(a => a.Usage == SupportedOperations.SerialiseOnly || a.Usage == SupportedOperations.SerialiseDeserialise))
                {
                    result.SerialisableTypes.Add(t);
                }
                if (attributes.Any(a => a.Usage == SupportedOperations.DeserialiseOnly || a.Usage == SupportedOperations.SerialiseDeserialise))
                {
                    result.DeserialisableTypes.Add(t);
                }
            }
            var referencedAssemblyNames = GetFilteredReferencedAssemblyNames(assembly.GetReferencedAssemblies());

            foreach (var refAssemblyName in referencedAssemblyNames)
            {
                if (_usedAssemblies.Contains(refAssemblyName.Name))
                {
                    continue;
                }
                var refAssembly = TryLoadAssembly(dir, refAssemblyName);
                if (refAssembly == null)
                {
                    Debug.WriteLine($"Cannot load assembly {refAssemblyName.Name}");
                    continue;
                }
                GetSerialisables(refAssembly, dir, result, depth + 1);
            }
        }
Esempio n. 2
0
        public DasherAssemblyInfo GetDasherAssemblyInfo(Assembly assembly)
        {
            _usedAssemblies = new HashSet <string>();
            var dir    = Path.GetDirectoryName(assembly.Location);
            var result = new DasherAssemblyInfo();

            GetSerialisables(assembly, dir, result);
            return(result);
        }
Esempio n. 3
0
        private static void WriteToManifest(
            DasherAssemblyInfo dasherAssemblyInfo, string outputDirectoryName, string outputFileName, string rootElementTag, string serialisableTypeElementTag,
            string serialisesTypesElementTag, string deserialisesTypesElementTag)
        {
            XDocument doc;
            // Create a file with a root element if the manifest doesn't exist. This should only happen in testing.
            string   manifestFileName = outputDirectoryName + outputFileName;
            XElement rootElement;

            if (File.Exists(manifestFileName))
            {
                doc         = XDocument.Load(manifestFileName);
                rootElement = doc.Element(rootElementTag);
                if (rootElement == null)
                {
                    rootElement = new XElement(rootElementTag);
                    doc.AddFirst(rootElement);
                }
                if (rootElement.Element(serialisesTypesElementTag) == null)
                {
                    rootElement.Add(new XElement(serialisesTypesElementTag));
                }
                if (rootElement.Element(deserialisesTypesElementTag) == null)
                {
                    rootElement.Add(new XElement(deserialisesTypesElementTag));
                }
            }
            else
            {
                doc         = new XDocument(new XElement(rootElementTag));
                rootElement = doc.Element(rootElementTag);
                rootElement.Add(new XElement(serialisesTypesElementTag));
                rootElement.Add(new XElement(deserialisesTypesElementTag));
            }

            var serialisesTypesElement   = new XElement(serialisesTypesElementTag);
            var deserialisesTypesElement = new XElement(deserialisesTypesElementTag);

            foreach (var serialisableType in dasherAssemblyInfo.SerialisableTypes)
            {
                var element = XMLSchemaGenerator.GenerateSchema(serialisableType, serialisableTypeElementTag);
                serialisesTypesElement.AddFirst(element);
            }
            foreach (var deserialisableType in dasherAssemblyInfo.DeserialisableTypes)
            {
                var element = XMLSchemaGenerator.GenerateSchema(deserialisableType, serialisableTypeElementTag);
                deserialisesTypesElement.AddFirst(element);
            }

            rootElement.Element(serialisesTypesElementTag).ReplaceWith(serialisesTypesElement);
            rootElement.Element(deserialisesTypesElementTag).ReplaceWith(deserialisesTypesElement);

            doc.Save(manifestFileName);
        }