C#从嵌套类访问更高级别的方法

C#从嵌套类访问更高级别的方法,c#,nested-class,C#,Nested Class,我想为通过串行端口进行通信的外部控制电路创建一个库类。电路具有内置功能,可使用串行通信获取/设置各种设置(例如,发送“SR,HC,01,1,\r”可打开传感器1)。大约有100个功能分为以下几类:传感器设置、输出设置和环境设置。这是我试过的 public class CircuitController { // Fields. private SerialPort controllerSerialPort; private SensorSettings sensorSettin

我想为通过串行端口进行通信的外部控制电路创建一个库类。电路具有内置功能,可使用串行通信获取/设置各种设置(例如,发送“SR,HC,01,1,\r”可打开传感器1)。大约有100个功能分为以下几类:传感器设置、输出设置和环境设置。这是我试过的

public class CircuitController
{
   // Fields.
   private SerialPort controllerSerialPort;
   private SensorSettings sensorSettings;
   private OutputSettings outputSettings;
   private EnvironmentSettings environmentSettings;
   ...

  // Properties.
  // Properties to get sensorSettings, outputSettings, and environmentSettings.

  // Methods.
  public string SendReceive(string sendCommand)   // Send command and receive response.
  {
     ...
  }

  // Nested classes.
  public class SensorSettings
  {
     // Fields.
     // The various sensor settings here.

     // Properties.
     // Properties to get/set the sensor settings. Note: Get/Set is done through calling one of the following methods.

     // Methods.
     public double GetSensorUnits(int sensorNumber)
     {
        ...
        string commandToSend = String.Format("HE,WL,1,{0}", sensorNumber);   // Setup command string.
        string commandResponse = SendReceive(commandToSend);   // Send command and receive response. ERROR here, cannot access higher level, non-static methods.
        // Logic to process commandResponse.
        ...
     }

     // Other methods to create, send, and process the circuit's sensor settings "functions".

  }

  public class OutputSettings
  {
     // Same logic as SensorSettings class.
  }

  public class EnvironmentSettings
  {
     // Same logic as SensorSettings class.
  }
}
我认为这样一来,
CircuitController
类下就不会塞满100个方法/属性。例如,我可以使用get属性获取sensorSettings实例,然后调用所需的方法/属性:
circuitControllerInstance.GetSensorSettingsProperty.GetSensorUnits(1)。我收到一个编译错误,试图从嵌套类访问
SendReceive()
。有办法做到这一点吗

谢谢

嵌套类不会“看到”其宿主中声明的任何内容


您应该传递对任何嵌套类的主机引用,例如,在构造函数中。

如果您感兴趣,Microsoft开发类库的设计指南中有避免公共嵌套类型的一般建议:是否有完整源代码示例工作的最终解决方案?