【问题标题】:C# Is it possible to go to given line of from one file to another?C# 是否可以从一个文件转到另一个文件的给定行?
【发布时间】:2017-03-06 14:53:55
【问题描述】:

我想问是否可以在 C# 中从一个类转到另一个类的给定行。这是我的想法的一个例子:

public class Class1
{
  public void MethodFromClass1()
  {
     // Doing some operations before to go in the another class
     goto: PlaceToGo
     // Doing some operations after going in the another class
  }
}

public class Class2
{
  public void MethodFromClass2()
  {
     PlaceToGo:
        Console.WriteLine("Print something.");
  }
}

【问题讨论】:

  • 如果你需要这样做,那你就大错特错了。 C# 是一种面向过程和面向对象的语言。您应该熟悉这些范例。
  • 根据您在代码中的 cmets,您只需要一个正常的方法调用,而不是典型的 goto 行为,只是去某个地方而不回来。
  • 我不想开始下一个“goto 是邪恶的”讨论,但你在这里尝试的是反对所有 C#。只需调用 MethodFromClass2。为什么转到?
  • 同样不可能 - Reference: "如果当前函数成员中不存在具有给定名称的标签,或者如果 goto 语句不在标签,就会发生编译时错误。”

标签: c# class oop goto


【解决方案1】:

使用goto 运算符不是一个好习惯。你应该避免它。在您的情况下,您可以通过另一种方式来做到这一点(创建 Class2 的实例并调用其方法):

public void MethodFromClass1()
{
     Class2 class2 = new Class2();
     class2.MethodFromClass2();
}

另外,如果你甚至跳到这个类,对象可能还没有被创建,所以它没有意义。

【讨论】:

  • 为什么这不是一个好习惯?该功能的存在是有原因的。请提供一些论据。
【解决方案2】:

您可以熟悉 goto :https://msdn.microsoft.com/en-us/library/13940fs2.aspx

“跳转”到不同的班级对我来说没有太大意义,因为在此之前它从未“构建”过。无论如何,我建议您不要在 C# 中使用这种技术,因为它会使核心难以跟踪和理解。

【讨论】:

    【解决方案3】:

    goto 的少数合法用法之一可能是在 switch 语句中:

    // Though this is a very stupid example!
    int number = 0;
    
    switch (number)
    {
        case 0:
           Console.Write("hello ");
           goto case 1;
    
        case 1:
           Console.WriteLine("world!");
           break;
    }
    

    顺便说一句,我从来没有发现自己使用过goto,而你的用例简直是荒谬的。

    也许你想要的是一个事件,这就是广播一个heya!我正要做点什么,有人吗?,一些监听器执行动作作为对整个事件的响应。请参阅以下代码 sn-p 以获取更多详细信息:

    public class Class1
    {
      private event EventHandler _BeforeDoingStuff;
      private event EventHandler BeforeDoingStuff 
      {
          add { _BeforeDoingStuff += value; }
          remove { _BeforeDoingStuff -= value; }
      }
    
      public void DoStuff()
      {
         // Do some stuff
         // Then fire the event
         _BeforeDoingStuff?.Invoke(EventArgs.Empty);
         // Continue with more stuff after firing the event
      }
    }
    
    public class Class2
    {
      public Class2(Class1 class1) 
      {
          class1.BeforeDoingStuff += (sender, e) => 
          {
               Console.WriteLine("I did some stuff in the middle of Class1.DoStuff!");
          }
      }
    }
    
    Class1 class1 = new Class1();
    Class2 class2 = new Class2(class1);
    

    详细了解 C# here 中的事件。

    【讨论】:

      【解决方案4】:

      这应该描述你想要的。

      using System;
      
      namespace ConsoleApp1
      {
          class FirstMethod
          {
              public void ExecuteThisNow(int number)
              {
                  Console.WriteLine("This number: " + number + " is going to be skipped over");
              }
          }
      }
      
      namespace ConsoleApp1
      {
          class SecondMethod
          {
              private int lengthOfLoop = 10;
      
              private int i = 0;
      
              public void RunTheLoop()
              {
                  for (this.i = 0; i < this.lengthOfLoop; i++)
                  {
                      if (this.i == 5)
                      {
                          goto ExecuteFirstMethod;
                      }
                      else
                          continue;
                  }
                  ExecuteFirstMethod: FirstMethod first = new FirstMethod();
                  first.ExecuteThisNow(this.i);
              }
          }
      }
      
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      
      namespace ConsoleApp1
      {
          using System;
      
          class Program
          {
              static void Main(string[] args)
              {
                  var second = new SecondMethod();
                  second.RunTheLoop();
                  Console.ReadLine();
              }
          }
      }
      

      【讨论】:

        【解决方案5】:

        当然,这样的代码闻起来很糟糕,而且是非常糟糕的做法。但如果你正在玩一个简单的游戏/测试,你可以这样做:

        public interface IGoTo
        {
            void Goto(params object[] p);
        }
        
        public class CalleeClass : IGoTo
        {
            public void Goto(params object[] p)
            {
                this.MethodFromCallee();
            }
        
            public void MethodFromCallee()
            {
        
            }
        }
        
        public class CallerClass
        {
            public void MethodFromCaller(IGoTo g)
            {
                g.Goto();
            }
        }
        
        static void Main(string[] args)
        {
            var callee = new CalleeClass();
            new CallerClass().MethodFromCaller(callee);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-16
          • 2018-11-04
          • 2017-05-24
          • 2019-08-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多