C# 限制代码仅在数据符合要求时触发?

C# 限制代码仅在数据符合要求时触发?,c#,C#,我已经编写了一个系统,在触发警报时发送电子邮件。但是,我想根据发送对象自定义发送内容。每个人都有一个唯一的位置,用数字表示(即000102)。我想要一个在location=x时执行的代码字符串。以下是我提取位置的代码: if (IN_PRODUCTION && sendAlert) { //get location, assetDetail and system of dataSensor MainS

我已经编写了一个系统,在触发警报时发送电子邮件。但是,我想根据发送对象自定义发送内容。每个人都有一个唯一的位置,用数字表示(即000102)。我想要一个在location=x时执行的代码字符串。以下是我提取位置的代码:

if (IN_PRODUCTION && sendAlert)
  {                                 
  //get location, assetDetail and system of dataSensor
   MainSystem dataSensorMainSystem = null;
   Subsystem dataSensorSubsystem = null;
   SystemComponent dataSensorSystemComponent = null;
    Location location = null;

     if (dataSensor.SystemComponent != null)
       {
        dataSensorSystemComponent = dataSensor.SystemComponent;
       if (dataSensorSystemComponent.Subsystem != null)
       {
        location = dataSensorSystemComponent.Subsystem.MainSystem.Location;
       }
        else
       {
       location = dataSensorSystemComponent.MainSystem.Location;
       }
       }
       else if (dataSensor.Subsystem != null)
       {
       dataSensorSubsystem = dataSensor.Subsystem;
       location = dataSensorSubsystem.MainSystem.Location;
       }
       else
       {
       dataSensorMainSystem = dataSensor.MainSystem;
       location = dataSensorMainSystem.Location;
       }

我有接下来的代码,但我希望它只在拉取的位置与预设数字匹配时执行。我怎么能做到这一点呢?

我真的不明白,你是说
如果(位置==“000102”){…}
?有那么简单吗?差不多。我本来想打那个,但我觉得没用。让我试试。我真的不明白,你的意思是
如果(位置==“000102”){…}
?有这么简单吗?差不多。我本来想打那个,但我觉得没用。让我试试。