有条件的使用块 [英] Conditional using block

查看:70
本文介绍了有条件的使用块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想听听您对条件using 块的意见.问题是我需要具有如下结构:

Hi,

I''d like to hear your opinions on conditional using blocks. The thing is that I need to have a structure like the following:

public void AMethod(bool useClass2) {
   using (Class1 variable1  = new Class1()) {
      using (Class2 variable2 = new Class2(variable1) {
         using (Class3 variable3 = new Class3(variable2) {
            variable3.DoSomething();
         }
      }
   }
}


现在的问题是,根据方法的参数,应该包括或排除中间的using块.如果参数useClass2true,则应按上述方式执行代码,但如果参数为false,则执行应如下所示:


Now the problem is that based on the parameters of the method the middle using block should either be included or excluded. If the parameter useClass2 is true, the code should be executed as above but if the parameter is false, then the execution should look like the following:

public void AMethod(bool useClass2) {
   using (Class1 variable1  = new Class1()) {
         using (Class3 variable3 = new Class3(variable1) {
            variable3.DoSomething();
         }
   }
}


因此,根据参数,可以包括中间的using块,而最里面的using块则使用Class1或Class2.

现在,可以使用例如:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆