#unity/日常积累 # AppDomain.GetAssemblies 方法 - 参考 [](https://learn.microsoft.com/zh-cn/dotnet/api/system.appdomain.getassemblies?view=net-6.0#definition) ## 定义 命名空间: [System](https://learn.microsoft.com/zh-cn/dotnet/api/system?view=net-6.0) 程序集: System.Runtime.dll 获取已加载到此应用程序域的执行上下文中的程序集。 ``` cs public System.Reflection.Assembly[] GetAssemblies (); ``` #### 返回 [Assembly](https://learn.microsoft.com/zh-cn/dotnet/api/system.reflection.assembly?view=net-6.0)[] 此应用程序域中的程序集的数组。 [](https://learn.microsoft.com/zh-cn/dotnet/api/system.appdomain.getassemblies?view=net-6.0#--) [](https://learn.microsoft.com/zh-cn/dotnet/api/system.appdomain.getassemblies?view=net-6.0#--) #### 例外 [AppDomainUnloadedException](https://learn.microsoft.com/zh-cn/dotnet/api/system.appdomainunloadedexception?view=net-6.0) 在卸载的应用程序域上尝试该操作。 [](https://learn.microsoft.com/zh-cn/dotnet/api/system.appdomain.getassemblies?view=net-6.0#--) ## 示例 下面的代码示例使用 [GetAssemblies](https://learn.microsoft.com/zh-cn/dotnet/api/system.appdomain.getassemblies?view=net-6.0) 该方法获取已加载到应用程序域的所有程序集的列表。 然后,程序集会显示到控制台。 若要运行此代码示例,需要创建一 `CustomLibrary.dll`个名为的程序集,或更改传递给该方法的 [GetAssemblies](https://learn.microsoft.com/zh-cn/dotnet/api/system.appdomain.getassemblies?view=net-6.0) 程序集名称。 ``` cs using System; using System.Reflection; using System.Security.Policy; class ADGetAssemblies { public static void Main() { AppDomain currentDomain = AppDomain.CurrentDomain; //Provide the current application domain evidence for the assembly. Evidence asEvidence = currentDomain.Evidence; //Load the assembly from the application directory using a simple name. //Create an assembly called CustomLibrary to run this sample. currentDomain.Load("CustomLibrary",asEvidence); //Make an array for the list of assemblies. Assembly[] assems = currentDomain.GetAssemblies(); //List the assemblies in the current application domain. Console.WriteLine("List of assemblies loaded in current appdomain:"); foreach (Assembly assem in assems) Console.WriteLine(assem.ToString()); } } ```