溫馨提示×

C#中如何加載和卸載Assembly

c#
小樊
112
2024-08-12 04:48:37
欄目: 編程語言

在C#中,可以使用Assembly.Load方法來加載一個Assembly,使用Assembly.Unload方法來卸載一個Assembly。

加載Assembly的示例代碼如下:

Assembly assembly = Assembly.Load("AssemblyName");

卸載Assembly的示例代碼如下:

AppDomain.CurrentDomain.GetAssemblies().ToList()
    .ForEach(a =>
    {
        if (a.FullName.StartsWith("AssemblyName"))
        {
            AppDomain.CurrentDomain.Load(a.FullName);
            AppDomain.CurrentDomain
                .GetAssemblies()
                .Where(
                    currentAssembly =>
                        currentAssembly.FullName == a.FullName
                )
                .ToList()
                .ForEach(currentAssembly =>
                {
                    AppDomain.CurrentDomain
                        .Load(currentAssembly.FullName)
                        .GetTypes()
                        .ToList()
                        .ForEach(
                            type =>
                            {
                                if (type != null)
                                    currentAssembly.CreateInstance(
                                        type.FullName
                                    );
                            }
                        );
                });
        }
    });

請根據(jù)實際情況調(diào)整代碼中的Assembly名稱和邏輯。

0