C# 我的控制器中的非静态字段、方法或属性需要对象引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14141323/
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-10 10:49:39  来源:igfitidea点击:

An object reference is required for non-static field, method, or property in my controller

c#asp.netasp.net-mvcasp.net-mvc-4

提问by Guilherme Longo

Possible Duplicate:
An object reference is required for the non-static field, method, or property

可能重复:
非静态字段、方法或属性需要对象引用

I have a non-static field:

我有一个非静态字段:

private IDictionary<string, string> _mappings = 
    new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)

that I wnat to use inside of a action like this:

我想在这样的动作中使用:

public static string GetMimeType(string extension)
{
    //...
    return _mappings.TryGetValue(extension, out mime) ? 
        mime : "application/octet-stream";
}

The compiler is complaining:

编译器抱怨:

An object reference is required for non-static field, method, or property in the return statement.

return 语句中的非静态字段、方法或属性需要对象引用。

How can I reference this field?

如何引用此字段?

采纳答案by Trevor Pilley

You cannot access instance members from static members so you have 2 choices.

您无法从静态成员访问实例成员,因此您有 2 个选择。

  1. Make the method an instance method (remove the statickeyword)
  2. Make the field a static (add the statickeyword)
  1. 使方法成为实例方法(去掉static关键字)
  2. 使字段成为静态(添加static关键字)

The one you choose will depend on whether the field should be shared across all instances or not.

您选择的那个取决于是否应该在所有实例之间共享该字段。

回答by Gage Trader

I think the compiler is pretty clear here: Your GetMimeType method is a static method, but the _mappings variable is not declared static (an non-static or instance field/variable).

我认为编译器在这里很清楚:您的 GetMimeType 方法是静态方法,但 _mappings 变量未声明为静态(非静态或实例字段/变量)。

If you want to use the mappings variable as it appears above do this:

如果您想使用上面显示的 mappings 变量,请执行以下操作:

private static IDictionary<string, string> _mappings = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)

Edit: As the commenter pointed out below, you must be careful that this is actually the behavior you want. A static member means all instances will share this same mappings variable and can overwrite the data present. If you want one mappings variable per class, then you should change your method to an instance method (by removing the static keyword), as noted in the answer above.

编辑:正如评论者在下面指出的那样,您必须注意这实际上是您想要的行为。静态成员意味着所有实例都将共享相同的映射变量,并且可以覆盖存在的数据。如果您希望每个类有一个映射变量,那么您应该将您的方法更改为实例方法(通过删除 static 关键字),如上面的答案所述。

回答by JLRishe

If you just want a dictionary of values that is populated once and never modified after that, a thing you could do would be make the dictionary static and populate it in a static constructor.

如果您只想要一个填充一次且此后永远不会修改的值的字典,您可以做的一件事是使字典静态并将其填充到静态构造函数中。