Ejemplo n.º 1
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;
            }
        }
Ejemplo n.º 2
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));
        }