简体   繁体   English

使用C#自动实现的属性编译错误

[英]Compile error using C# automatically implemented properties

AC# Noob, I am trying to use SharpDevelop utility to convert my VB.NET application a C#. AC#Noob,我正在尝试使用SharpDevelop实用程序将VB.NET应用程序转换为C#。

I am noticing that my automatically implemented properties are generating a lot of errors. 我注意到我的自动实现属性会产生很多错误。 For example, take the following property: 例如,采用以下属性:

public SqlDateTime DateOfBirth { get; 公开SqlDateTime DateOfBirth {get; set; 组; } }

Whenever I try to access the implied underlying module level variable, _DateOfBirth, I get the error. 每当我尝试访问隐含的基础模块级别变量_DateOfBirth时,都会收到错误消息。

Error 699 The name '_DateOfBirth' does not exist in the current context D:\\Users\\Chad\\Desktop\\BESI CSharp\\BESI\\BESI.BusinessObjects.ConvertedToC#\\ChinaVisa.cs 240 13 Besi.BusinessObjects.Converted 错误699名称'_DateOfBirth'在当前上下文D中不存在D:\\ Users \\ Chad \\ Desktop \\ BESI CSharp \\ BESI \\ BESI.BusinessObjects.ConvertedToC#\\ ChinaVisa.cs 240 13 Besi.BusinessObjects.Converted

I could expand the properties declarations out to full-fledged properties, but this should n't be necessary and I'd like to understand why I am getting this error. 我可以将属性声明扩展为完整的属性,但这不是必须的,我想了解为什么会出现此错误。

You cannot access the compiler-created backing variable - you must use the property. 您无法访问由编译器创建的后备变量-必须使用该属性。 The compiler-generated backing field is specifically named in such a way to prevent you from accessing it (it is not named _DateOfBirth , it is named something like <DateOfBirth>k__BackingField ). 编译器生成的后备字段以一种防止您访问它的方式专门命名(它没有命名为_DateOfBirth ,它的名称类似于<DateOfBirth>k__BackingField )。

Access the property directly - if you need to manipulate the backing field directly then don't use an automatically implemented property. 直接访问属性-如果您需要直接操作后备字段,则不要使用自动实现的属性。

Just a side note - it doesn't matter what the property name is (it is an implementation detail and could change on different versions of the compiler or with different compiler implementations altogether). 只是一点说明-属性名称是什么都没有关系(它是一个实现细节,可以在不同版本的编译器上或完全在不同的编译器实现上进行更改)。 The field is given an identifier that is specifically designed to meet the naming restrictions of the CLR but fail the naming restrictions of C# making it impossible to ever write straight C# code that accesses that variable directly. 该字段被赋予一个标识符,该标识符是专门为满足CLR的命名限制而设计的,但是没有满足C#的命名限制,因此不可能编写直接访问该变量的直接C#代码。

Also remember that automatically implemented properties are not public fields. 还要记住,自动实现的属性不是公共字段。 They are a shorthand that the compiler expands for you (sort of like a macro). 它们是编译器为您扩展的简写(有点像宏)。

So this class: 所以这个课:

class Bar
{
    public object Foo { get; set; }
}

Gets expanded to this: 扩展为:

class Bar
{
    [CompilerGenerated]
    private object <Foo>k__BackingField;

    public object Foo
    {
        [CompilerGenerated]
        get
        {
            return this.<Foo>k__BackingField;
        }
        [CompilerGenerated]
        set
        {
            this.<Foo>k__BackingField = value;
        }
    }
}

It is still a full property - you are simply allowing the compiler to write the getters and setters for you. 它仍然是一个完整的属性-您只是允许编译器为您编写getter和setter。

If you use automatically generated properties, you cannot rely on the compiler picking any specific name for the backing field. 如果使用自动生成的属性,则不能依赖编译器为后备字段选择任何特定名称。 That's entirely compiler-specific, and even if you could access those fields, your code might still break if you use another compiler. 这完全是特定于编译器的,即使您可以访问这些字段,但是如果您使用其他编译器,您的代码仍可能会中断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM