0

我正在尝试加载 externalAssembly以列出所有TypesMethods并且Properties在运行时,就像Object Browserin一样Visual Studio。我的要求是,我想将Assembly其加载到外部列表Memberstreeview并在使用后卸载Assembly或卸载AppDomain。稍后在同一个项目中,我使用它assembly来修改和添加更多代码。由于我已经打开了程序集,CompileAssemblyFromFile因此不允许我编译和生成输出。我该如何处理这种情况?如何Object BrowserVisual Studio不创建实例的情况下工作,Assembly或者如果创建了实例,使用后如何卸载它?

附言

我已经尝试使用另一个AppDomain来加载程序集但没有成功。由于treeview是在主 appdomain 中创建的,因此我无法在自定义 appdomain 中访问它来添加节点。

4

1 回答 1

1

基于 Mathieu Guindon 的评论。我在我的 ASP.Net Web API 中加载程序集并将程序集内容序列化为 JSON 数组。稍后在我的客户端应用程序中,我使用帮助 HttpClient 反序列化响应并填充 WinForms TreeView。

我是这样做的。示例代码。

//Action Model
public class ActionModel
{
   public string ActionName {get;set;}
   public IList<MethodInformation> ActionMethods {get;set;}
}

public class MethodInformation
{
   public string MethodName {get;set;}
   public IList<ParameterInformation> GetParameters {get;set;}
}

//API
public Object Get(string id)
{
    switch(id)
    {
        case "Assembly1":
           Type[] typesInAssembly = Assembly.LoadFrom(pathToAssembly).GetTypes();
           
           List<ActionModel> actionModel = new List<ActionModel>();
           
           for(var t in typesInAssembly)
           {
               /* add the items to the List actionModel */
           }
           return actionModel;
     }
}

在客户端,我还有一个使用此 API 并通过反序列化 JSON 内容来解析内容并将其显示在树视图中的类。

现在满足了要求。我可以使用相同的 Assembly 副本来显示和构建主要要求。希望我做对了。

于 2022-02-08T07:17:00.510 回答