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

时间:2015-04-13 18:38:46

标签: 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";
}

这是不对其进行评估的代码行

Uri uri = new Uri(String.Format(ConstsConfig.BASE_URL_FORMAT, NetworkConfig.PROTOCOL, NetworkConfig.HOST, NetworkConfig.BASE_URL)));

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

我会想象它的东西很小。感谢您的帮助。

1 个答案:

答案 0 :(得分:9)

There is a long-standing debugging issue in Xamarin.Android with Visual Studio related to inspecting values in static classes. Specifically, if you set a breakpoint on a line referencing a static class (or a non-static class with static members), Visual Studio may show the inspection value as "Unknown identifier: [ClassName]".

From my analysis, it turns out that the locations of class files in the project determine whether or not you'll have that issue.

The upshot for me is that, until Xamarin fixes the bug, all static classes, and classes with static members, should be placed in the root folder of the project. There are other file placement options, but some flat out don't work, and one requires fully qualifying your static class call with the namespace--even when not required by the compiler.

See comments in code below for full details.

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"
        */
    }
}
}

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;
//}    
}

On 4/3/2016, I updated the associated Xamarin Bugzilla ticket with this information. Hopefully they get this resolved soon.