[英]Visual studio VSPackage behavior different after installation
I have developped a Visual studio 2010 VSPackage in C#. 我已经用C#开发了Visual Studio 2010 VSPackage。 It's role is to show an assembly types and methods in a Treeview.
它的作用是在Treeview中显示程序集类型和方法。 It uses reflexion like this :
它使用像这样的反射:
Assembly assembly = Assembly.LoadFile(strAssemblyPath);
foreach (Type a_type in assembly.GetTypes())
{
foreach (MethodInfo mi in a_type.GetMethods())
{
//code to handle methods here
}
}
I have several Assemblies that reference others, all situated in the same folder. 我有几个引用其他组件的程序集,它们都位于同一文件夹中。
It works fine when I debug the application : the getTypes() and getMethods() don't raise any errors when trying to load types from others assemblies. 当我调试应用程序时,它运行良好:尝试从其他程序集中加载类型时,getTypes()和getMethods()不会引发任何错误。
When I generate a .vsix installer, (debug or release), and use the plugin afer installing it, the getTypes() and GetMethods() raise errors of type : 'Could not load file or assembly or one of its dependencies' but the assemblies are in the folder... 当我生成.vsix安装程序(调试或发行版)并在安装后使用插件时,getTypes()和GetMethods()会引发以下类型的错误:“无法加载文件或程序集或其依赖项之一”,但程序集在文件夹中...
Some more informations : 更多信息:
Does anyone know why the behavior of the GetTypes() and GetMethods() is different ? 有谁知道为什么GetTypes()和GetMethods()的行为不同?
Well, I still don't know why the behavior is different, but I was able to limit the impact of the error with correct error handling. 好吧,我仍然不知道为什么行为会有所不同,但是我能够通过正确的错误处理来限制错误的影响。 The error handling I use is this ;
我使用的错误处理是这样;
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
}
}
Before, I just catched the exception and didn't handle the types defined in the ReflectionTypeLoadException.Types. 以前,我只是捕获了异常,并且没有处理ReflectionTypeLoadException.Types中定义的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.