为什么这个集合初始值设定项试图是静态的?

发布于 2024-10-10 09:33:56 字数 1400 浏览 1 评论 0原文

我正在尝试创建一个非常简单的实体列表,用于我正在编写的一些单元测试,但编译器抱怨我无法从静态上下文访问实例变量。据我所知,只有实例变量。

using System;
using System.Collections.Generic;
using EMP.Domain.Entities;
using NUnit.Framework;

namespace EMP.Domain.Tests.Repositories
{
    [TestFixture]
    public class AuditTrailRepositoryTests
    {
        private Guid orgId1 = Guid.NewGuid();

        private IList<AuditTrail> fakeAuditData = new List<AuditTrail>
                                                      {
                                                          new AuditTrail
                                                              {
                                                                  ChangeDate = new DateTime(2011, 1, 1),
                                                                  EntityName = "Test",
                                                                  OrganisationId = orgId1,
                                                                  Username = "admin"
                                                              }
                                                      };
    }
}

这实际上就是此时类中的所有代码。编译器抱怨说

错误 1 ​​字段初始值设定项无法引用非静态字段、方法或属性“EMP.Domain.Tests.Repositories.AuditTrailRepositoryTests.orgId1”

是什么导致 fakeAuditData 认为它在静态上下文中运行?我什至添加了一个实例方法,并且能够访问集合和指南。那么初始化时集合是静态的吗?这是否意味着它只初始化一次?

I am trying to create a very simple list of entities that I am using for some unit tests I'm writing, but the compiler is complaining that I can't access an instance variable from a static context. So far as I can see, there are only instance variables.

using System;
using System.Collections.Generic;
using EMP.Domain.Entities;
using NUnit.Framework;

namespace EMP.Domain.Tests.Repositories
{
    [TestFixture]
    public class AuditTrailRepositoryTests
    {
        private Guid orgId1 = Guid.NewGuid();

        private IList<AuditTrail> fakeAuditData = new List<AuditTrail>
                                                      {
                                                          new AuditTrail
                                                              {
                                                                  ChangeDate = new DateTime(2011, 1, 1),
                                                                  EntityName = "Test",
                                                                  OrganisationId = orgId1,
                                                                  Username = "admin"
                                                              }
                                                      };
    }
}

That is literally all the code in the class at this point. The compiler is complaining that

Error 1 A field initializer cannot reference the non-static field, method, or property 'EMP.Domain.Tests.Repositories.AuditTrailRepositoryTests.orgId1'

What is causing the fakeAuditData to think it's running in a static context? I've even added an instance method and been able to access both the collection and the Guids. So is the collection static when initialised and does this mean that it is only initialised once?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小女人ら 2024-10-17 09:33:56

这个问题实际上与集合初始化器本身无关;这是因为在提供的示例中,集合初始化程序是实例字段初始化程序的一部分。这些不允许引用任何实例成员。根据语言规范:

10.5.5.2 实例字段初始化

类的实例字段变量初始值设定项对应于在进入任何一个实例构造函数时立即执行的一系列赋值(第 10.11.1 节) )该类的。变量初始值设定项按照它们在类声明中出现的文本顺序执行。类实例创建和初始化过程在第 10.11 节中进一步描述。

实例字段的变量初始值设定项无法引用正在创建的实例。因此,在变量初始值设定项中引用 this 是一个编译时错误,因为变量初始值设定项通过简单名称引用任何实例成员都是一个编译时错误。

The problem isn't really related to collection-initializers per se; it's do with the fact that it in the provided sample, the collection-initializer is part of an instance field-initializer. These are not allowed to reference any instance members. From the language specification:

10.5.5.2 Instance field initialization

The instance field variable initializers of a class correspond to a sequence of assignments that are executed immediately upon entry to any one of the instance constructors (§10.11.1) of that class. The variable initializers are executed in the textual order in which they appear in the class declaration. The class instance creation and initialization process is described further in §10.11.

A variable initializer for an instance field cannot reference the instance being created. Thus, it is a compile-time error to reference this in a variable initializer, as it is a compile-time error for a variable initializer to reference any instance member through a simple-name.

岁吢 2024-10-17 09:33:56

您可以使用 TestFixtureSetUp 为每个单元测试:

[TestFixture]

public class AuditTrailRepositoryTests
{
    private Guid orgId1;
    private IList<AuditTrail> fakeAuditData;

    [TestFixtureSetUp] 
    public void Init()
    {
        orgId1 = Guid.NewGuid();
        fakeAuditData = new List<AuditTrail>
        {
            new AuditTrail
            {
                ChangeDate = new DateTime(2011, 1, 1),
                EntityName = "Test",
                OrganisationId = orgId1,
                Username = "admin"
            }
        };
    }
}

You could use TestFixtureSetUp to prepare the fake data for each unit test:

[TestFixture]

public class AuditTrailRepositoryTests
{
    private Guid orgId1;
    private IList<AuditTrail> fakeAuditData;

    [TestFixtureSetUp] 
    public void Init()
    {
        orgId1 = Guid.NewGuid();
        fakeAuditData = new List<AuditTrail>
        {
            new AuditTrail
            {
                ChangeDate = new DateTime(2011, 1, 1),
                EntityName = "Test",
                OrganisationId = orgId1,
                Username = "admin"
            }
        };
    }
}
咿呀咿呀哟 2024-10-17 09:33:56

这也是编译时错误的相同原因:

class Foo
{
  int a = 5;
  int b = a;
}

The same reason why this is also a compile time error:

class Foo
{
  int a = 5;
  int b = a;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文