How to load a .NET assembly for reflection operations and subsequently unload it?

后端 未结 5 827
青春惊慌失措
青春惊慌失措 2020-11-28 06:11

I\'m writing a tool to report information about .NET applications deployed across environments and regions within my client\'s systems.

I\'d like to read the values

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 06:33

    From the MSDN documentation of System.Reflection.Assembly.ReflectionOnlyLoad (String) :

    The reflection-only context is no different from other contexts. Assemblies that are loaded into the context can be unloaded only by unloading the application domain.

    So, I am afraid the only way to unload an assembly is unloading the application domain. To create a new AppDomain and load assemblies into it:

    public void TempLoadAssembly()
    {
        AppDomain tempDomain = AppDomain.CreateDomain("TemporaryAppDomain");
        tempDomain.DoCallBack(LoaderCallback);
        AppDomain.Unload(tempDomain);
    }
    
    private void LoaderCallback()
    {
        Assembly.ReflectionOnlyLoad("YourAssembly");
        // Do your stuff here
    }
    

提交回复
热议问题