gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:13:17 25 4
gpt4 key购买 nike

当我尝试在此 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”或“未知标识符 NetworkConfig”

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

最佳答案

使用 Visual Studio 的 Xamarin.Android 中存在一个长期存在的调试问题,与检查静态类中的值有关。具体来说,如果您在引用静态类(或具有静态成员的非静态类)的行上设置断点,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"
*/
}
}
}

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 票证。希望他们能尽快解决这个问题。

关于c# - 使用常量 C# 时的未知标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29612645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com