【问题标题】:Visual studio VSPackage behavior different after installation安装后 Visual Studio VSPackage 行为不同
【发布时间】:2014-07-30 15:52:23
【问题描述】:

我用 C# 开发了一个 Visual Studio 2010 VSPackage。它的作用是在 Treeview 中显示程序集类型和方法。它使用这样的反射:

Assembly assembly = Assembly.LoadFile(strAssemblyPath);  
foreach (Type a_type in assembly.GetTypes())  
{  
    foreach (MethodInfo mi in a_type.GetMethods())  
    {  
        //code to handle methods here  
    }  
}  

我有几个引用其他程序集的程序集,都位于同一个文件夹中。

当我调试应用程序时它工作正常:getTypes() 和 getMethods() 在尝试从其他程序集中加载类型时不会引发任何错误。

当我生成 .vsix 安装程序(调试或发布)并在安装后使用插件时,getTypes() 和 GetMethods() 会引发以下类型的错误: “无法加载文件或程序集或其依赖项之一”但程序集在文件夹中...

更多信息:

  • 我使用我的 VSPackage 解决方案的默认设置。
  • 调试中的命令行是“C:\Program Files (x86)\Visual Studio 2010 Ultimate\Common7\IDE\devenv.exe /rootsuffix Exp”
  • 在调试和安装包之后,我尝试对完全相同文件夹中完全相同的程序集执行相同的操作。
  • Visual Studio 在这两种情况下都以管理员身份启动

有谁知道为什么 GetTypes() 和 GetMethods() 的行为不同?

【问题讨论】:

    标签: c# .net visual-studio-2010 vspackage


    【解决方案1】:

    好吧,我仍然不知道为什么行为会有所不同,但我能够通过正确的错误处理来限制错误的影响。我使用的错误处理是这样的;

    Type[] types = null;
    try
    {
        types = assembly.GetTypes();
    }
    catch (ReflectionTypeLoadException e)
    {
        types = e.Types;
        StringBuilder strErrors = new StringBuilder();
        foreach (Exception exError in e.LoaderExceptions)
        {
            //Message errors can appear several times.
            if (!strErrors.ToString().Contains(exError.Message))
            {
                 strErrors.Append(exError.Message);
                 Exception innerError = exError.InnerException;
                 while (innerError != null)
                 {
                     strErrors.Append(innerError.Message);
                     innerError = innerError.InnerException;
                 }
            }
        }
        //trace the error
    }
    if (types != null)
    {
        foreach (Type a_type in types)
        {
             //handle types in here
        }
    }
    

    之前,我只是捕获了异常,并没有处理 ReflectionTypeLoadException.Types 中定义的类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多