返回之前或之前触发代码

时间:2014-09-11 07:45:24

标签: c#

如何在返回之前/之后执行某些代码部分。例如,可以在方法中多次调用return,因此我希望在返回之前不复制粘贴相同的行。

我可能错了,但有些人告诉我,try block会使代码运行缓慢,因此我的方法被调用超过1000次,因此即使这个任务可以通过try / finally块完成,我也想避免它。

示例:

void method()
{
 MyObject activator = new ...
 AnotherObject something = new ...
 SomethingElse asdf = new ...
 // some other variables 
 if(..)
    // Deactivate activator, close things, confirm user exited
    // Do some post calculations
   return;
 if(..)
    // Deactivate activator, close things, confirm user exited
    // Do some post calculations
   return;
 if(..)
    // Deactivate activator, close things, confirm user exited
    // Do some post calculations
  return;
}

现在我需要在每次返回之前或之后执行相同的代码。我需要使用在方法顶部定义的变量的代码,这就是我无法外包的原因。这该怎么做?有办法吗?如果有重复,我会道歉。

3 个答案:

答案 0 :(得分:4)

我不清楚你想要实现什么,但根据你的帖子,你可以使用可怕的goto声明!

public void Test()
{
    if (...)
        goto Finish;
    if (...)
        goto Finish;

    Finish:
    DoSomething();  
}

根据您的更新,我肯定会考虑使用finally块:

void Main()
{
    try 
    {
     MyObject activator = new ...
     AnotherObject db_connection = new ...
     Proxy p = new ...
     // some other variables 
     if(..)
      return;
     if (...)
        return;
    }
    finally
    {
        // Deactivate activator, close db connection, call a webservice to confirm user exited
        // Do some post calculations
    }
}

尝试终极的效率

try-finally模式非常有效。请考虑以下代码:

try
{
    Console.WriteLine("Foo");
}
finally
{
    Console.WriteLine("Bar");
}

这将编译为以下IL:

IL_0000:  ldstr       "Foo"
IL_0005:  call        System.Console.WriteLine
IL_000A:  leave.s     IL_0017
IL_000C:  ldstr       "Bar"
IL_0011:  call        System.Console.WriteLine
IL_0016:  endfinally  

用英语表示:

  

加载字符串“Foo”并用它调用WriteLine。我们有finally语句,所以当我们调用它时,请转到位置IL_0017 - 方法的结尾。加载字符串“Bar”并用它调用WriteLine。现在终于完成了,所以我们可以继续到finally块之前定义的位置。

答案 1 :(得分:1)

也许考虑尝试终极建设。它用于可以抛出异常的代码路径,但是仍然必须始终进行清理(即使在抛出异常之后)。

http://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx

void method() {
     MyObject activator = new ...
     AnotherObject db_connection = new ...
     Proxy p = new ...
     try{
         // some other variables 
         if(..)
            // Do some post calculations
         if(..)
            // Do some post calculations
         if(..)
            // Do some post calculations
    }
    finally{
        // Deactivate activator, close db connection, call a webservice to confirm user exited
    }
      return;
}

// 更新

我想知道如果try / finally的性能太重而且会破坏您的应用程序,那么您是否应该重新设计应用程序或代码路径。您似乎在很短的时间内打开并关闭了1000多次连接。这是连接到1000多个数据库,还是连接到数据库的1000多个用户?为什么不保持开放?你能分享一些关于用例的其他细节吗?

答案 2 :(得分:0)

我还不清楚你在找什么。 但我明白的是。您希望使用值实例化少数对象,并且在移出方法之前,您希望处理/断开它们。

所以这是我用控制台应用程序尝试过的, 您可以使用Disposed()方法处理/断开您的值。

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Callmethod();
            Console.WriteLine("--------------------");
            Console.WriteLine("Disposed");
            Console.ReadKey();
        }

        private static string Callmethod()
        {
            using (MyObject obj = new MyObject())
            {
                Console.WriteLine(obj.strTest);
                Console.WriteLine("..");
                Console.WriteLine("..");
                Console.WriteLine("..");
                Console.WriteLine("..");
                return obj.strTest;
            }
        }
    }
    public class MyObject : IDisposable
    {
        public string strTest { get; set; }
        public MyObject()
        {
            strTest = "Intantiated";
        }

        public void Dispose()
        {
            Console.WriteLine("Disposing");
        }
    }
}

希望这有帮助

相关问题