VS 2008调试:System.ArgumentException?

3
问题: 我有一个表单项目,它实例化了在另一个dll项目中定义的类。当运行使用该dll的表单应用程序时,一切都运行得非常好, 但是,当我设置断点以检查在dll项目中定义的类型的对象时,在我的监视窗口中出现错误。
一些可能重要的事情:
  1. dll项目使用不安全和非托管代码。
  2. 在调用任何不安全函数之前就会出现此问题。
  3. 启用了非托管代码调试。
  4. 我知道符号已加载到dll中。
  5. 我知道调试器加载的dll版本与应用程序使用的版本相同。
  6. 我已清理并删除了输出目录,然后重新构建。
  7. 它们使用相同的.NET版本。
示例:如果我将MyDllType.SomeProperty添加到我的监视窗口中,则会在监视窗口中看到以下内容: 'MyDllType.SomeProperty' threw an exception of type 'System.ArgumentException' Message: "Cannot find the method on the object instance." 然而,如果我在完全相同的位置添加Debug.Writeline(MyDllType.SomeProperty);,那么我将不会收到任何异常,并且它将在输出控制台中正确显示。 此外:如果我创建一个在dll项目中定义的结构体类型的列表,并将其添加到我的监视窗口中,则会在监视窗口中看到以下内容:
// My Dll Project
public struct MyDllStruct
{
    public int x;
    public int y;
    public int z;
}

// Snippet from a function block in forms project
List<MyDllStruct> structList = new List<MyDllStruct>();

// Add a bunch of stuff to the list
// <-- Insert a breakpoint here
}

当我在监视窗口中添加并打断structList时,我会得到以下错误信息:无法计算表达式,操作不支持,未知错误:0x8004f0ed。 然而,如果我在同一点上再添加Debug.Writeline(structList.Count);,那么就不会出现异常,并且计数将正确显示在输出控制台中。
完整示例:
// My Dll Project
public class MyDllType
{
    public MyDLLType()
    {
        this.someProperty = 123456;
    }

    private int someProperty;
    public int SomeProperty
    {
        get{ return this.someProperty; }
        set{ this.someProperty = value; }
    }
}
public struct MyDllStruct
{
    public int x;
    public int y;
    public int z;
}

// My Forms Project
public class SomeController
{
    public SomeController()
    {
        this.dllType = new DllType();
        List<MyDllStruct> structList = new List<MyDllStruct>();

        // <-- For example, I get both aformentioned problems if i break here (or anywhere else these objects have been instantiated)
    }

    private MyDllType dllType;
}

你可能已经想象到了,这使得调试我的应用程序变得非常困难 :) 欢迎提供任何帮助。

2个回答

4
这不是一个答案,而是关于Watch窗口与Debug打印输出之谜的一些见解。
Visual Studio中的Watch窗口使用内置的托管表达式求值器对您的输入进行评估,比如说structList。作为运行时解析器,它完全不同于编译器本身,并且仅理解表达式的子集。您可以在这里阅读到一个很好的描述。
因此,有可能 - 我模糊地记得自己也遇到过这种情况 - 这个表达式求值器无法正确处理来自不安全DLL的类型。作为“轻量级编译器”,它确实存在一些缺陷,这可能是其中之一。
另一方面,Debug.WriteLine()正常工作,因为传递给它的表达式由C#编译器自己在编译时处理。

0

你能检查一下你的配置管理器并确认所有项目都编译为“Any CPU”吗?这可能有点冒险,但我知道有人遇到了类似的问题,我模糊地记得他的项目设置是混合了x86和Any CPU。


是的,两者都设置为“Any CPU”。 - tbridge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接