C# 如何解决此“资源名称不是有效标识符”编译器警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2274073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:57:19  来源:igfitidea点击:

How to resolve this "Resource name is not a valid identifier" compiler warning

c#resourcescompiler-warnings

提问by Kevin

I've got a bunch of Warnings in a Visual Studio 2005 project, most of them are warning me that a resource name is not a valid identifier.

我在 Visual Studio 2005 项目中有一堆警告,其中大部分警告我资源名称不是有效标识符。

example:

例子:

The resource name 'MB_ArchiveRestore.cs_11' is not a valid identifier.

MSDN online helpindicates that the resource name needs to be strongly typed, and not contain spaces. What does strongly typed exactly mean?

MSDN在线帮助提示资源名称需要强类型,不能包含空格。强类型到底是什么意思?

采纳答案by Daniel Brückner

Strongly typed means that a variable, field, or property is of a specific type instead of just Object.

强类型意味着变量、字段或属性具有特定类型,而不仅仅是Object.

public class User
{
    public String FirstName { get; set; } // Strongly typed
    public Object LastName { get; set; } // Weakly typed
}

If you use strongly typed resources, code is generated with strongly typed properties for all your resources. In this case the resource name is used as the property name, hence it must be a valid C# property name. Your example MB_ArchiveRestore.cs_11contains a dot and is in consequence not a valid property name. The code generator will replace the dot with an underscore to make the name valid and gives you the described warning to inform you about that.

如果您使用强类型资源,则会为您的所有资源生成具有强类型属性的代码。在这种情况下,资源名称用作属性名称,因此它必须是有效的 C# 属性名称。您的示例MB_ArchiveRestore.cs_11包含一个点,因此不是有效的属性名称。代码生成器将用下划线替换点以使名称有效,并向您提供所描述的警告以通知您。

回答by Ian

I'm not sure if it will help you fix your problem but in the interest of answering your actual question. Strongly typed means that the the variable is of a given type, as opposed to some type determined at run time. Check out Wikipedia

我不确定它是否会帮助您解决问题,但为了回答您的实际问题。强类型意味着变量是给定的类型,而不是在运行时确定的某种类型。查看维基百科

For example:

例如:

Int32 counter;

Means the counter variable is strongly typed, as we know it is an Int32. Other languages or use of dynamic keywords mean:

意味着计数器变量是强类型的,因为我们知道它是一个 Int32。其他语言或使用动态关键字意味着:

dynamic counter = ResultOfFunc()

means that counter is not strongly typed, as it is determined at run time by the result of the ResultOfFunc().

意味着 counter 不是强类型的,因为它在运行时由 ResultOfFunc() 的结果确定。

回答by John Feminella

The problem occurs because .is not a valid character in identifiers.

出现问题是因为.标识符中的字符无效。

What does strongly typed exactly mean?

强类型到底是什么意思?

Although it's not as relevant for this particular question, "strongly typed" means that an object has a definite notion of type. For example, you can't do int i = "5";in C#, because "5" is a string and iis an integer -- their types are incompatible with each other.

尽管它与这个特定问题无关,但“强类型”意味着对象具有明确的类型概念。例如,你不能int i = "5";在 C# 中做,因为 "5" 是一个字符串并且i是一个整数——它们的类型彼此不兼容。

This is contrast to "weakly typed" languages, where the notion of "type" is not as strong. A weakly typed language might decide that for something like i = 5; j = "6"; print (i + j);, the correct response is 11.

这与“弱类型”语言形成对比,其中“类型”的概念不那么强。弱类型语言可能会决定对于诸如 之类的东西i = 5; j = "6"; print (i + j);,正确的响应是 11。

回答by Marek

Based on the link you have posted in the question, I think that you are probably asking about strongly typed resource generation- that means that Visual Studio will generate a resources file which will allow you to access resources via typed properties, e.g.

根据您在问题中发布的链接,我认为您可能正在询问strongly typed resource generation- 这意味着 Visual Studio 将生成一个资源文件,该文件将允许您通过类型化属性访问资源,例如

string fileName = Resources.FileName;
bool someSetting = Resources.AllowDelete;
byte[] binaryResource = Resources.SomeFile;

as opposed to untyped resources where you have to cast the return value by yourself because it returns type System.Object instead of a specific type.

与您必须自己转换返回值的无类型资源相反,因为它返回类型 System.Object 而不是特定类型。

string fileName = (string)Resources["FileName"];
bool someSetting = (bool)Resources["AllowDelete"];
byte[] binaryResource = (byte[])Resources["SomeFile"]

回答by Tarka

I would have to guess that it's complaining about the _11 at the end, which would make it not a valid C# file, thus, not a valid resource.

我不得不猜测它在末尾抱怨 _11,这将使它不是有效的 C# 文件,因此不是有效的资源。

回答by Derick Bailey

"Strongly Typed" in this case, means that Visual Studio is trying to generate an object model for you to use, from your resource names.

在这种情况下,“强类型”意味着 Visual Studio 正在尝试根据您的资源名称生成供您使用的对象模型。

For example, say you have a resource file with URLs that point to your favorite websites. your resources are something like:

例如,假设您有一个资源文件,其中的 URL 指向您最喜欢的网站。您的资源类似于:

when the resource generate is used to strongly type this, you will end up with an object model that can be called like this:

当资源 generate 用于强类型 this 时,您将最终得到一个可以像这样调用的对象模型:

string googleUrl = Resources.Google; string msUrl = Resources.Microsoft;

string googleUrl = Resources.Google; string msUrl = Resources.Microsoft;

when you have a period in the name of the resource, the code generator cannot use it because it would create invalid names in the Resources object. for exmaple:

当资源名称中有句点时,代码生成器将无法使用它,因为它会在 Resources 对象中创建无效名称。例如:

this would be invalid because it would try to create a resource named Resources.Asp.NET

这将是无效的,因为它会尝试创建一个名为 Resources.Asp.NET

回答by rotti2

I guess that you have a resource named "MB ArchiveRestore.cs 11". Since VS 2005 the compiler (or more precisely an add-on tool) will automatically generate access classes for embedded resources. These can be used to retrieve the resources. The class property for your example is probably Properties.Resources.MB_ArchiveRestore.cs_11. These stronlgy typed resource classes also provide a convenient way for localization.

我猜您有一个名为“MB ArchiveRestore.cs 11”的资源。从 VS 2005 开始,编译器(或者更准确地说是一个附加工具)将自动生成嵌入式资源的访问类。这些可用于检索资源。您的示例的类属性可能是Properties.Resources.MB_ArchiveRestore.cs_11. 这些强类型资源类还提供了一种方便的本地化方法。