使用常量时的未知标识符C#

CBa*_*ker 7 c# visual-studio-debugging

当我试图在这个C#应用程序中使用常量时.当我运行调试器时,常量出现为"未知标识符"继承代码

public static class ConstsConfig
{
    public static string BASE_URL_FORMAT = "%s://%s%s";
}

public static class NetworkConfig
{
    public static string PROTOCOL = "http";
    public static string HOST = "www.example.com";
    public static string BASE_URL = "/test";
}
Run Code Online (Sandbox Code Playgroud)

这是它没有评估它的代码行

Uri uri = new Uri(String.Format(ConstsConfig.BASE_URL_FORMAT, NetworkConfig.PROTOCOL, NetworkConfig.HOST, NetworkConfig.BASE_URL)));
Run Code Online (Sandbox Code Playgroud)

所以,当我单步执行调试器并在此行中断开时.如果你超过其中一个常数.它只是说"未知标识符ConstsConfig"或"Uknown identifier NetworkConfig"

我会想象它的东西很小.我在这里先向您的帮助表示感谢.

Rob*_*uce 9

Xamarin.Android中存在一个长期调试问题,Visual Studio与检查静态类中的值有关.具体来说,如果在引用静态类(或具有静态成员的非静态类)的行上设置断点,则Visual Studio可能会将检查值显示为"未知标识符:[ClassName]".

根据我的分析,事实证明项目中类文件的位置决定了你是否会遇到这个问题.

对我来说,结果是,在Xamarin修复错误之前,所有静态类和具有静态成员的类都应该放在项目的根文件夹中.还有其他文件放置选项,但有些扁平放置不起作用,并且需要使用命名空间完全限定静态类调用 - 即使编译器不需要.

有关详细信息,请参阅下面代码中的注释

MainActivity.cs

using System;
using Android.App;
using Android.OS;

namespace App1 {

[Activity(Label = "Unknown Identifier Test", MainLauncher = true)]
public class MainActivity : Activity {        

    protected override void OnCreate(Bundle bundle) {
        base.OnCreate(bundle);

        Console.WriteLine(MyClass.MyString);            // Unqualified
        Console.WriteLine(App1.MyClass.MyString);       // Fully Qualified with namespace

        /*
        Set a break point on the "Console.WriteLine()" lines above and you'll get the 
        "Unknown identifier: MyClass" error when trying to inspect under specific conditions...

        File Locations                                      Unqualified             Fully Qualified
        -------------------------------------------------   ---------------------   --------------------
        MainActivity.cs in root, MyClass.cs in sub-folder   "Unknown identifier"    Inspection Works
        MainActivity.cs in sub-folder, MyClass.cs in root   Inspection Works        Inspection Works
        Both in root                                        Inspection Works        Inspection Works
        Both in different sub-folders                       "Unknown identifier"    "Unknown identifier"
        Both in same sub-folder                             "Unknown identifier"    "Unknown identifier"
        */
    }
}
}
Run Code Online (Sandbox Code Playgroud)

MyClass.cs

namespace App1 {
public static class MyClass {
    public static string MyString;
}

// The class can also be constructed this way, which results in the same findings:
//public class MyClass {
//    public static string MyString;
//}    
}
Run Code Online (Sandbox Code Playgroud)

2016年4月3日,我使用此信息更新了相关的Xamarin Bugzilla票证.希望他们很快得到解决.