コード例 #1
0
        private static LibraryPack Load(
            IEnumerable <WinLibRecord> winLibs,
            IEnumerable <UnixLibRecord> unixLibs
            )
        {
            var(osName, arch, bitDesign) = EnvironmentAuxiliary.GetEnvironment();

            var libraryPack = new LibraryPack();

            try
            {
                if (osName == OSName.Windows)
                {
                    if (winLibs == null)
                    {
                        throw new Exception("Not find native libraries for Windows");
                    }
                    foreach (var winLib in winLibs)
                    {
                        WinLoadedLibrary loadedLibrary = null;
                        try
                        {
                            var path = winLib.GetLibrary((arch, bitDesign));
                            loadedLibrary = new WinLoadedLibrary(path);
                            libraryPack.AddLibrary(loadedLibrary);
                        }
                        catch (Exception exc)
                        {
                            loadedLibrary?.Dispose();
                            throw exc;
                        }
                    }
                }
                else
                {
                    throw new NotImplementedException(
                              $"Not implement loading for os '{osName}");
                }
            }
            catch (Exception exc)
            {
                libraryPack.Dispose();
                throw exc;
            }

            return(libraryPack);
        }
コード例 #2
0
        /// <summary>
        /// Initialize Type's, delegates and nested classes entry points from
        /// dynamic libs (so, dll), where entry points mangled name contain C++ scope
        /// (namespaces, classes and others) like
        /// nestedClassType::nestedClassType2::entry_function_name.
        /// </summary>
        /// <param name="type">
        /// Top Level Type, who contain scopes in static nested classes (Types) or
        /// instances, but type not present a C++ scope
        /// </param>
        /// <param name="winLibs">
        /// list of windows pathes to dynamic libs, who need loading before working logic of Type
        /// will start to work. order of dynamic libs loading determined by order of in pathe's list.
        /// </param>
        /// <param name="unixLibs">
        /// list of unix (include linux) pathes to dynamic libs, who need loading before working logic of Type
        /// will start to work. Order of dynamic libs loading determined by order of in pathes list.
        /// </param>
        public static void InitializeWithScope(
            Type type,
            IEnumerable <WinLibRecord> winLibs,
            IEnumerable <UnixLibRecord> unixLibs
            )
        {
            // validation
            var libraryPackField = type.GetField("_libraryPack", BindingFlags.NonPublic | BindingFlags.Static);

            if (libraryPackField == null || libraryPackField.FieldType != typeof(LibraryPack))
            {
                throw new Exception(
                          $"${type} not has static field _libraryPack or type of _libraryPack is not {nameof(LibraryPack)}"
                          );
            }

            LibraryPack libraryPack = null;

            try
            {
                libraryPack = Load(winLibs, unixLibs);

                var(osName, arch, bitDesing) = EnvironmentAuxiliary.GetEnvironment();
                var entryPointSearcher = EntryPointSearchProvider.EntryPointSearcher(osName);
                var bindingLib         = libraryPack.LoadedLibraries.Last();

                var entries = entryPointSearcher.EnumerateEntryPoints(bindingLib.Path);

                BindMethodsWithScope(
                    type,
                    null,
                    bindingLib,
                    entries);

                libraryPackField.SetValue(null, libraryPack);
            }
            catch (Exception exc)
            {
                libraryPack?.Dispose();
                throw exc;
            }
        }