Пример #1
0
        // - - -

        /// <summary>
        /// Find modifiers of this assembly.
        /// </summary>
        /// <param name="assembly"></param>
        /// <returns></returns>
        public static ModifiersAssembly GetModifiersAssembly(Assembly assembly)
        {
            ModifiersAssembly ret = null;

            if (!_ModifiersAssemblyCache.TryGetValue(assembly, out ret))
            {
                ret = new ModifiersAssembly(assembly);
                _ModifiersAssemblyCache.Add(assembly, ret);
            }
            return(ret);
        }
Пример #2
0
        internal void Init()
        {
            // Adds the caller's modifiers to the list.
            {
                var callerAssembly = Tools.GetEntryAssembly();
                if (callerAssembly != null)
                {
                    var ma = ModifiersAssembly.GetModifiersAssembly(callerAssembly);
                    if (this.ModifiersAssemblies == null)
                    {
                        this.ModifiersAssemblies = new ModifiersAssembly[1] {
                            ma
                        }
                    }
                    ;
                    else
                    if (!this.ModifiersAssemblies.Contains(ma))
                    {
                        var mas = new List <ModifiersAssembly>(this.ModifiersAssemblies);
                        mas.Add(ma);
                        this.ModifiersAssemblies = mas.ToArray();
                    }
                }
            }

            // Adds the internal modifiers to the list.
            if (this.ModifiersAssemblies == null)
            {
                this.ModifiersAssemblies = new ModifiersAssembly[1] {
                    UniversalSerializer.InternalModifiersAssembly
                }
            }
            ;
            else
            if (!this.ModifiersAssemblies.Contains(UniversalSerializer.InternalModifiersAssembly))
            {
                var mas = new List <ModifiersAssembly>(this.ModifiersAssemblies);
                mas.Add(UniversalSerializer.InternalModifiersAssembly);
                this.ModifiersAssemblies = mas.ToArray();
            }

            {            // this.customModifiers is the aggregation of all modifiers of all listed assemblies, more internal.
                CustomModifiers cm = CustomModifiers.Empty;
                foreach (var ma in this.ModifiersAssemblies)
                {
                    cm = cm.GetCombinationWithOtherCustomModifiers(ma.aggregatedCustomModifiers);
                }
                this.customModifiers = cm;
            }
        }
Пример #3
0
        // - - -

        /// <summary>
        /// Find modifiers of an assembly.
        /// </summary>
        /// <param name="assemblyShortName">The assembly (short) name [not the file name].</param>
        /// <param>On (pure, not portable) Android, the DLL file must be added as an Asset.</param>
        /// <returns></returns>
        /// <exception cref="FileNotFoundException">Please note on .NET Core the current directory can be wrong. Please set it correctly before calling this function.</exception>
        public static ModifiersAssembly GetModifiersAssembly(string assemblyShortName)
        {
            Assembly assembly;

#if WINDOWS_UWP
            var an = new AssemblyName(assemblyShortName);
            assembly = Assembly.Load(an);
#else
#if SILVERLIGHT && !WINDOWS_PHONE7_1
            var name = assemblyShortName + ".dll";
            var uri  = new Uri(name, UriKind.Relative);
            var sm   = System.Windows.Application.GetResourceStream(uri);
            if (sm == null)
            {
                throw new FileNotFoundException(name);
            }
            var ap = new System.Windows.AssemblyPart();
            assembly = ap.Load(sm.Stream);
#else
#if ANDROID
            // On Android, the DLL files must be added as embedded resources to the application project.
            var ea   = Tools.GetEntryAssembly();          // finds the application assembly.
            var an   = ea.GetName();
            var an2  = an.Name;
            var name = an2 + "." + assemblyShortName + ".dll";
            //var r =ea.GetManifestResourceNames();

            using (var sm = ea.GetManifestResourceStream(name))
            {
                if (sm == null)
                {
                    throw new FileNotFoundException(name);
                }
                var bytes = new byte[sm.Length];
                sm.Read(bytes, 0, bytes.Length);
                assembly = Assembly.Load(bytes);
            }
#else // ordinary .NET
            try
            {
                assembly = Assembly.Load(assemblyShortName);
            }
            catch (FileNotFoundException ex)
            {
                // tries to read the dll file.
                var name = assemblyShortName + ".dll";
                if (File.Exists(name))
                {
                    assembly = Assembly.LoadFrom(name);
                }
                else
                {
                    // looks in the resources of all referenced assemblies for an embedded file.
                    assembly = null;

                    var ea = Tools.GetEntryAssembly();                     // finds the application assembly.
                    if (ea != null)
                    {
                        assembly = _LoadResourceAssembly(ea, assemblyShortName);
                    }

                    if (assembly == null)
                    {
                        // Searches the file in the resources of all loaded assemblies.
                        var assemblies = AppDomain.CurrentDomain.GetAssemblies();
                        for (int i = 0; i < assemblies.Length && assembly == null; i++)
                        {
                            Assembly rc = assemblies[i];
                            assembly = _LoadResourceAssembly(rc, assemblyShortName);
                        }
                    }

                    if (assembly == null)
                    {
                        throw ex;
                    }
                }
            }
#endif
#endif
#endif
            return(ModifiersAssembly.GetModifiersAssembly(assembly));
        }
Пример #4
0
        /// <summary>
        /// Serialize (another) data, using the same parameters, keeping types in common.
        /// </summary>
        /// <param name="data"></param>
        internal void SubSerialize(
            object data
            )
        {
#if !WINDOWS_PHONE && !PORTABLE && !NET3_5
            this.ClassInstanceIndexes = new ConditionalWeakTable <object, IndexClass>();
#else
            this.ClassInstanceIndexes = new Dictionary <object, IndexClass>(); // TODO: replace by something faster.
#endif
            this.ClassInstancesCount = 0;

            this.StartTree();

            // inserts Header: (new in version 3.0)
            {
                Header header = new Header();

                // inserts modifiers' assembly list:

                // Do not take in account this own assembly.
                ModifiersAssembly[] a1 = new ModifiersAssembly[parameters.ModifiersAssemblies.Length - 1];
                Array.Copy(parameters.ModifiersAssemblies, 0, a1, 0, a1.Length);
                header.AssemblyIdentifiers = a1.Select((a) => new AssemblyIdentifier(a.assembly)).ToArray();

                {                 // Inserts in stream:
                    var tm = this.l3typeManagerCollection.GetTypeManager(typeof(Header), this, true, false);
                    this.AddAnObject(
                        ref this.channelInfos[(int)ChannelNumber.InstancesChannel],
                        header,
#if DEBUG
                        "Header",
#else
                        null,
#endif
                        tm.TypeIndex,
                        tm,
                        false,                         // The header is not (officially) placed at root because it is not the main data.
                        false);
                }
            }

            // Serialize main data:
            {
                Type t = data != null?data.GetType() : typeof(object);

                var tm = this.l3typeManagerCollection.GetTypeManager(t, this, true, false);
                this.AddAnObject(
                    ref this.channelInfos[(int)ChannelNumber.InstancesChannel],                     // The main object is added as an instance, even when it s structure.
                    data,
                    null,
                    tm.TypeIndex,                     // Type is done for the root object.
                    tm,
                    tm.l2TypeManager.IsClass,         // structures are not placed at root because that would create an instance element <s1>, then during deserialization that would imply it is an instance (a class) but it is not.
                    true);
            }

            // serialize the type descriptors:
            if (parameters.SerializeTypeDescriptors && parameters.TheStreamingMode == StreamingModes.AssembledStream)
            {
                AddACollection(
                    ref this.channelInfos[(int)ChannelNumber.TypeDescriptorsChannel],
                    this.l3typeManagerCollection.GetSerializationTypeDescriptors(),
                    this.l3typeManagerCollection.GetTypeManager(typeof(SerializationTypeDescriptorCollection), null, true, false)
                    );
            }

            this.FinalizeTreeToStream();
        }