字段初始不能引用非静态字段,方法或属性? [英] A field initializer cannot reference the non-static field, method, or property?

查看:211
本文介绍了字段初始不能引用非静态字段,方法或属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个仓储类和服务类,如下:

 公共类DinnerRepository
{
    DinnerDataContext DB =新DinnerDataContext();    公共晚餐GetDinner(INT ID)
    {
        返回db.Dinners.SingleOrDefault(D => d.DinnerID == ID);
    }//其他code
}公共类服务
{
        DinnerRepository回购=新DinnerRepository();
        晚餐晚餐= repo.GetDinner(5);//其他code
}

这将引发错误:


  

一个字段初始不能引用非静态字段,方法或属性。


尽管我已经intatiated的DinnerRepository类暴露了其在服务类中的方法GetDinner()。这工作得很好低于code。有什么替代它,或者它是一个标准的做法?我不能在这里使用静态方法。

 公共类服务
{    公共服务()
    {
        DinnerRepository回购=新DinnerRepository();
        晚餐晚餐= repo.GetDinner(5);
    }}


解决方案

个人而言,我只希望初始化字段的构造:

 公共类服务
{
    私人只读DinnerRepository回购;
    私人晚宴只读晚宴;    公共服务()
    {
        回购=新DinnerRepository();
        晚餐= repo.GetDinner(5);
    }
}

请注意,这是不一样的code你展示在问题的底部,因为这是唯一宣布的本地的变量。如果你只的希望的局部变量,这很好 - 但如果你需要的实例的变量,然后用code如上

基本上,字段初始在他们能做什么限制。从C#4规范第10.5.5.2:


  

有关实例字段不能引用实例变量初始创建。因此,它是引用一个编译时错误这个在一个变量初始化,因为它是一个编译时错误的变量初始化通过一个引用任何实例成员的简单名称


(即因此和所以轮看起来是错误的方式我 - 这是非法通过一个简单名称引用成员的,因为的它引用这个 - 我会Ping一下它的Mads - 但是这基本上是相关章节)

I have a Repository Class and a Services Class as below :

public class DinnerRepository
{
    DinnerDataContext db = new DinnerDataContext();

    public Dinner GetDinner(int id)
    {
        return db.Dinners.SingleOrDefault(d => d.DinnerID == id);   
    }

// Others Code        
}



public class Service
{
        DinnerRepository repo = new DinnerRepository(); 
        Dinner dinner = repo.GetDinner(5);

// Other Code
}

This throws error:

A field initializer cannot reference the non-static field, method, or property.

Even though I have intatiated the DinnerRepository Class to expose its method GetDinner() in the Service Class. This works fine with below code. Is there any alternative to it or is it a standard practice? I cannot use static methods here..

public class Service
{

    public Service()
    {
        DinnerRepository repo = new DinnerRepository(); 
        Dinner dinner = repo.GetDinner(5);
    }

}

解决方案

Personally I'd just initialize the fields in a constructor:

public class Service
{
    private readonly DinnerRepository repo;
    private readonly Dinner dinner;

    public Service()
    {
        repo = new DinnerRepository();
        dinner = repo.GetDinner(5);
    }
}

Note that this isn't the same as the code you show at the bottom of the question, as that's only declaring local variables. If you only want local variables, that's fine - but if you need instance variables, then use code as above.

Basically, field initializers are limited in what they can do. From section 10.5.5.2 of the C# 4 spec:

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, because it is a compile-time error for a variable initializer to reference any instance member through a simple-name.

(That "thus" and "therefore" looks the wrong way round to me - it's illegal to reference a member via a simple-name because it references this - I'll ping Mads about it - but that's basically the relevant section.)

这篇关于字段初始不能引用非静态字段,方法或属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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