Visual studio 2008 VS 2008中即时窗口的特殊行为
今天在VS2008中调试时发生了一些奇怪的事情。我将给出这个小代码片段Visual studio 2008 VS 2008中即时窗口的特殊行为,visual-studio-2008,debugging,immediate-window,Visual Studio 2008,Debugging,Immediate Window,今天在VS2008中调试时发生了一些奇怪的事情。我将给出这个小代码片段 List<IPageHandler> myPageList = TaskSOM.PageList; if( myPageList != null && myPageList.Count > 0 ) { PageHandler aPage = myPageList[0] as PageHandler; ...; // Some more code below } a
List<IPageHandler> myPageList = TaskSOM.PageList;
if( myPageList != null && myPageList.Count > 0 )
{
PageHandler aPage = myPageList[0] as PageHandler;
...; // Some more code below
}
aPage变量具有适当的值。但如果将调试器移动到该行并执行,则会得到一个null。由于保密的原因,我无法共享整个代码。但在过去,有没有人在眼前的窗口中遇到过这样的问题。是否有关于即时窗口如何工作的资料。这将是一个非常好的代码示例,说明您不想使用as运算符。显然,您无法承受强制转换失败的代价,或者如果强制转换失败,您将包含一个空测试并执行一些有意义的操作 使用真正的演员阵容。您将得到一个信息丰富的异常,它可以更好地提示强制转换失败的原因:
PageHandler aPage = (PageHandler)myPageList[0];
例外是你的朋友,不要回避它们。胡乱猜测:当您在线程中使用COM对象而COM服务器不支持封送处理时,可能会发生这种情况。如果是这种情况,则异常消息会告诉您。因此,以下是完整的详细信息。例外是
[A] SimpleClassLib.PageHandler无法强制转换为[B]SimpleClassLib.PageHandler。类型A源自位置“D:…\bin\SimpleClassLib.dll”的上下文“LoadNone”中的“SimpleClassLib,版本=1.0.0.0,区域性=中性,PublicKeyToken=null”。类型B源自位置“D:…\bin\Debug\SimpleClassLib.dll”的上下文“Default”中的“SimpleClassLib,版本=1.0.0.0,区域性=中立,PublicKeyToken=null”
开发人员在一个应用程序配置文件中提到了[A]D:…\bin\SimpleClassLib.dll,并使用[B]D:…\bin\Debug\SimpleClassLib.dll构建了真正的应用程序,因此应用程序的一部分从[A]创建了PageHandler实例并填充了列表,另一部分尝试从[B]键入cast to PageHandler
下面的示例将很容易触发此错误。希望这对别人有帮助。
这是一个简单的类库。将其构建为dll
// SimpleClassLib.dll
namespace SimpleClassLib
{
public class Foo
{
string Prop1 { get { return "I am Foo!!"; } }
}
}
以下是控制台应用程序。该应用程序链接到SimpleClassLib,就像VS2008中的普通添加引用一样。它还从另一个路径加载实例
// Separate console application App.exe
// Progoram.cs
using SimpleClassLib;
namespace App
{
class Program
{
List<object> myFooList;
Program()
{
myFooList = new List<object>();
Assembly a = Assembly.LoadFile(@"<differentpath>\SimpleClassLib.dll");
Type aFooType = a.GetType("SimpleClassLib.Foo");
ConstructorInfo aConstructor = aFooType.GetConstructor(new Type[] { });
myFooList.Add(aConstructor.Invoke(new object[]{}));
myFooList.Add(aConstructor.Invoke(new object[] { }));
myFooList.Add(aConstructor.Invoke(new object[] { }));
}
void DumpPeculiar()
{
for (int i = 0; i < myFooList.Count; i++)
{
// If one inspects the list in debugger will see a list of
// Foo but this Foo comes from a different load context so the
// following cast will fail. While if one executes the line
// f = myFooList[i] as Foo
// it will succeed
Foo f = myFooList[i] as Foo;
Foo f1 = (Foo)myFooList[i];
}
}
static void Main(string[] args)
{
Program p = new Program();
p.DumpPeculiar();
}
}
}
//独立控制台应用程序App.exe
//Progoram.cs
使用SimpleClassLib;
命名空间应用程序
{
班级计划
{
列出我的傻瓜;
程序()
{
my傻瓜列表=新列表();
程序集a=Assembly.LoadFile(@“\SimpleClassLib.dll”);
类型aFooType=a.GetType(“SimpleClassLib.Foo”);
ConstructorInfo aConstructor=aFooType.GetConstructor(新类型[]{});
添加(aConstructor.Invoke(新对象[]{}));
添加(aConstructor.Invoke(新对象[]{}));
添加(aConstructor.Invoke(新对象[]{}));
}
无效转储文件()
{
for(int i=0;i
感谢您的快速回复。我发现了问题。我将添加另一个帖子来提供完整的细节。我已经给出了这个问题的答案。但我仍然不明白相同类型的cast如何在即时窗口中工作。只有在编写特殊的程序集转储工具时,才应该使用Assembly.LoadFile()。使用LoadFrom()。
// Separate console application App.exe
// Progoram.cs
using SimpleClassLib;
namespace App
{
class Program
{
List<object> myFooList;
Program()
{
myFooList = new List<object>();
Assembly a = Assembly.LoadFile(@"<differentpath>\SimpleClassLib.dll");
Type aFooType = a.GetType("SimpleClassLib.Foo");
ConstructorInfo aConstructor = aFooType.GetConstructor(new Type[] { });
myFooList.Add(aConstructor.Invoke(new object[]{}));
myFooList.Add(aConstructor.Invoke(new object[] { }));
myFooList.Add(aConstructor.Invoke(new object[] { }));
}
void DumpPeculiar()
{
for (int i = 0; i < myFooList.Count; i++)
{
// If one inspects the list in debugger will see a list of
// Foo but this Foo comes from a different load context so the
// following cast will fail. While if one executes the line
// f = myFooList[i] as Foo
// it will succeed
Foo f = myFooList[i] as Foo;
Foo f1 = (Foo)myFooList[i];
}
}
static void Main(string[] args)
{
Program p = new Program();
p.DumpPeculiar();
}
}
}