[英]c# Class Access from Nested Class Confusion
我創建了一個圖書館課程,其中...
public class CircuitLibrary
{
// Fields, properties, methods, etc.
...
// Nested classes.
public class Sensor
{
// Enums.
public enum Sensors { Sensor1, Sensor2, Sensor3, ... };
...
}
public class SerialCommands
{
// Fields, properties, etc.
...
// Nested classes.
public class SensorSettingsCommands
{
// Fields, properties, etc.
...
public void SomeMethod()
{
...
if( Sensor.Sensors.IsOn ) // Doesn't like this. OK if I change to CircuitLibrary.Sensor.Sensors.IsOn. Why?
...
}
}
}
}
這是我收到的錯誤:
Cannot access a nonstatic member of outer type
"MyCircuitLibrary.CircuitLibrary.SerialCommands" via nested type
"MyCircuitLibrary.CircuitLibrary.SerialCommands.SensorSettingsCommands"
如此看來,它正在SerialCommands
搜索(並找到?) Sensor
? 但是,如果我將其更改為CircuitLibrary.Sensor
它現在知道它在CircuitLibrary中? 當我單擊鼠標右鍵並單擊“轉到定義”時,它沒有問題,沒有說“在SerialCommands
找不到Sensor
”。 如果有人可以幫助解釋發生了什么,我將不勝感激。
謝謝!
您的SerialCommands
類具有非靜態Sensor
屬性。
由於此屬性比最外層的Sensor
類更接近您的代碼,因此編譯器認為您使用的是屬性而不是類。
由於在沒有SerialCommands
實例的情況下無法使用非靜態屬性,因此會出現錯誤。
當你編寫CircuitLibrary.Sensor
,它工作正常,因為沒有CircuitLibrary
屬性來混淆編譯器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.