使用常量 C# 时的未知标识符 [英] Unknown identifier when using constants C#

查看:36
本文介绍了使用常量 C# 时的未知标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When im trying to use constants in this C# application. When i run through the debugger, the constants come up as an "unknown identifier" Heres the code

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

这是没有评估它的代码行

This is the line of code where its not evaluating it seems like

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

所以当我单步调试调试器并在这条线上中断时.如果您徘徊在其中一个常数上.它只是说未知标识符 ConstsConfig"或未知标识符 NetworkConfig"

So when i step through the debugger and break on this line. If you hoever over one of the constants. It just says "Unknown identifier ConstsConfig" or "Uknown identifier NetworkConfig"

我会想象它很小.提前感谢您的帮助.

I would imagine its something small. Thanks for the help in advance.

推荐答案

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

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.

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

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

在 2016 年 4 月 3 日,我使用此信息更新了关联的 Xamarin Bugzilla 票证.希望他们能尽快解决这个问题.

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

这篇关于使用常量 C# 时的未知标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆