【DOTNET】注入アセンブリ

    ///  <まとめ> 
    /// IServiceCollectionは拡張
     ///  </要約> 
    パブリック 静的 クラスServiceExtension 
    { 
        ///  <まとめ> 
        /// DIバッチ噴射ハブアセンブリ対応する実装クラスです。
        ///  <パラ> 
        /// 注意次の規則がある、ということ:
         /// 。IUserService - > UserServiceの、IUserRepository - > UserRepository
         ///  </パラ> 
        ///  </要約> 
        // /  <PARAM NAME = " -サービス"> </ PARAM> 
        ///  <PARAM NAME = "interfaceAssemblyName"> ファイル拡張子なし)アセンブリインタフェースの名前</ PARAM> 
        ///  <
         静的 IServiceCollection RegisterAssembly(この IServiceCollectionサービス、文字列interfaceAssemblyName)
        { 
            場合(サービス== nullがスロー 新しい例外ArgumentNullExceptionを(がNameOf(サービス));
            もし文字列.IsNullOrEmpty(interfaceAssemblyName))
                 スロー 新しい例外ArgumentNullExceptionを(がNameOf(interfaceAssemblyName)); 

            VaRは、アセンブリ= RuntimeService.GetAssembly(interfaceAssemblyName)を、
            もし(アセンブリ== NULL 
            { 
                スロー 新しい DllNotFoundException($ " DLL \" {interfaceAssemblyName} \ "が見つかりません" ); 
            } 

            // 过滤掉非接口及泛型接口
            するvarタイプ= assembly.GetTypes()ここで、(T => t.GetTypeInfo()IsInterface &&。!。t.GetTypeInfo()IsGenericType。); 

            foreachのVARのタイプタイプ)
            { 
                VAR implementTypeName = type.Name.Substring(1 )。
                VAR implementType = RuntimeService.GetImplementType(implementTypeName、タイプ)。
                もし(implementType!= nullを
                    service.AddSingleton(タイプ、implementType); 
            } 
            戻り-サービスを; 
        } 

        ///  <まとめ> 
        /// 組立実装クラスを対応DI一括噴射ハブを有します。
        ///  </要約> 
        ///  <PARAM NAME = " -サービス"> </ PARAM> 
        ///  <PARAM NAME = "interfaceAssemblyName"> アセンブリインタフェースの名前(ファイル拡張子なし)</ PARAM> 
        / //  ます。<param name = "implementAssemblyName"> 達成アセンブリ名(ないファイルの拡張子)</ param>の
        ///  <リターンA> </リターン> 
        パブリック 静的 IServiceCollection RegisterAssembly(この IServiceCollectionサービス、文字列implementAssemblyName)
        { 
            場合(サービス== nullがスロー 新しい例外ArgumentNullExceptionを(がNameOf(サービス));
            もし文字列.IsNullOrEmpty(interfaceAssemblyName))
                 スロー 新しい例外ArgumentNullExceptionを(がNameOf(interfaceAssemblyName));
            もし文字列.IsNullOrEmpty(implementAssemblyName))
                 スロー 新しい例外ArgumentNullExceptionを(がNameOf(implementAssemblyName)); 

            VAR interfaceAssembly = RuntimeService.GetAssembly(interfaceAssemblyName)。
            もし(interfaceAssembly == nullの
            { 
                スロー 新しい DllNotFoundExceptionを($ " DLL \" "が見つかりません{interfaceAssemblyName} \ " ); 
            } 

            VAR implementAssembly = RuntimeService.GetAssembly(implementAssemblyName)。
            場合(implementAssembly == nullの
            { 
                スロー 新しい DllNotFoundExceptionを($ " DLL \" "が見つかりません{implementAssemblyName} \ " ); 
            } 

            // 过滤掉非接口及泛型接口
            VARタイプ= interfaceAssembly.GetTypes()ここで、(T => t.GetTypeInfo()IsInterface &&。!。t.GetTypeInfo()IsGenericType。); 

            foreachのVARのタイプタイプ)
            { 
                // 过滤掉抽象类、泛型类以及非クラス
                VAR implementType = implementAssembly.DefinedTypes 
                    .FirstOrDefault(トン => t.IsClass &&!t.IsAbstract &&!t.IsGenericType && 
                                         t.GetInterfaces ().ANY(B => b.Name == type.Name))。
                もし(!implementType = ヌル 
                {
                    service.AddSingleton(タイプ、implementType.AsType())。
                } 
            } 

            戻りサービス。
        } 
    }
 パブリック クラスRuntimeService 
    { 
        ///  <要約> 
        /// プロジェクトのアセンブリを取得し、アセンブリのすべてのシステムを除外する(マイクロソフト。***、システム。***など)、Nugetダウンロードパッケージ
         ///  </要約> 
        / //  <戻り値> </戻り値> 
        パブリック 静的のIList <アセンブリ> GetAllAssemblies()
        { 
            VARの一覧= 新しい新しいリスト<組立> ();
             VAR DEPS = DependencyContext.Default;
             VAR LIBS = deps.CompileLibraries.Where(LIB =>! && lib.Type = lib.Serviceable!" のためのパッケージ変更")。//は、すべてのシステム・アセンブリ、Nugetダウンロードパッケージを除外
            するforeachVARの LIB LIBS)
            { 
                試みる
                { 
                    VARアセンブリ= AssemblyLoadContext.Default.LoadFromAssemblyName(新規のAssemblyName(lib.Name))。
                    list.Add(アセンブリ)
                } 
                キャッチ(例外)
                { 
                    // 無視
                } 
            } 
            戻り値のリスト。
        } 

        パブリック 静的アセンブリGetAssembly(文字列のAssemblyName)
        { 
            戻り GetAllAssemblies()。FirstOrDefault(アセンブリ=>assembly.FullName.Contains(のAssemblyName))。
        } 

        パブリック 静的のIList <タイプ> GetAllTypes()
        { 
            VARのリスト= 新しいリスト<タイプ> ();
            foreachのVARアセンブリGetAllAssemblies())
            { 
                VARたtypeinfos = assembly.DefinedTypes。
                foreachのVAR TYPEINFO におけるたtypeinfos)
                { 
                    list.Add(typeInfo.AsType())。
                } 
            } 
            戻り値のリスト。
        } 

        パブリック 静的のIList <タイプ> GetTypesByAssembly(文字列のAssemblyName)
        { 
            VARのリスト= 新しいリスト<タイプ> ();
            VARアセンブリ= AssemblyLoadContext.Default.LoadFromAssemblyName(新しいのAssemblyName(のAssemblyName));
            VARたtypeinfos = assembly.DefinedTypes。
            foreachのVAR TYPEINFO におけるたtypeinfos)
            { 
                list.Add(typeInfo.AsType())。
            } 
            戻りリスト。
        } 

        公共の 静的タイプGetImplementType(文字列型名、型baseInterfaceType)
        { 
            戻り GetAllTypes()。FirstOrDefault(T => 
            { 
                場合(t.Name ==型名&& 
                    t.GetTypeInfo()。GetInterfacesメソッド()。任意の(B => b.Name == baseInterfaceType.Name))
                { 
                    VAR TYPEINFO = t.GetTypeInfo();
                     戻り!typeInfo.IsClass && typeInfo.IsAbstract && typeInfo.IsGenericType; 
                } 
                戻り ; 
            }); 
        } 
    }

おすすめ

転載: www.cnblogs.com/xuxml/p/12114887.html