C# (object) 是一个“字段”,但像“类型”一样使用

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

(object) is a 'field' but is used like a 'type'

c#asp.net

提问by iKode

My intellisense is giving me the error: 'ClassLibrary1.GetTimeZone.myWorldTime' is a 'field' but is used like a 'type'

我的智能感知给了我错误:“ClassLibrary1.GetTimeZone.myWorldTime”是一个“字段”,但像“类型”一样使用

Any idea what I'm doing wrong?

知道我做错了什么吗?

My code

我的代码

!Object Browser for the class1My code

!Object Browser for class 1我的代码

采纳答案by Louis Kottmann

You need to put the line in error in a class constructor or a method.

您需要在类构造函数或方法中将该行出错。

回答by Oded

LoadDatais a staticmethod. You call it on the type, not an instance.

LoadData是一个静态方法。你在类型上调用它,而不是一个实例。

ChaosSoftware.WorldTime.LoadData("worldtime.xml");

This needs to be placed inside a method in order to execute (constructor or other method).

这需要放置在方法中才能执行(构造函数或其他方法)。

Additionally, though not the reason for the error, you should use "to delimit a string. In C#, single quotes are for character literals (that is, single characters). What you have will not compile.

此外,虽然不是错误的原因,但您应该使用"来分隔字符串。在 C# 中,单引号用于字符文字(即单个字符)。你所拥有的不会编译。

回答by Simon Mourier

LoadData seems to be a static function, so you should do this instead (without an object instance):

LoadData 似乎是一个静态函数,所以你应该这样做(没有对象实例):

ChaosSoftware.WorldTime.LoadData("blahblah...");