简体   繁体   中英

Executing code dynamically in C#

I would like to dynamically control code execution using a pointer, basically a function pointer without the stack frame. Execution does not return to the calling location, but to a single point.

TopOfLoop:
...
Jump(x)


x1:
...
continue

x2:
...
continue

etc.

Is this possible in c#? Thanks.

Maybe by using goto statement?

Reference at MSDN: http://msdn.microsoft.com/en-us/library/13940fs2.aspx

Though, it's not recommended way of coding. Try to use delegates instead:

 Func<T,TResutl> or Action<T>

Sample:

using System;

public class LambdaExpression
{
   public static void Main()
   {
      Func<string, string> convert = s => s.ToUpper();

      string name = "RIA Guy";
      Console.WriteLine(convert(name));   
   }
}

In terms of function pointers, you'd probably want to have a look at delegates in c#. As to the rest of it, that has been covered by others.

这看起来也像SWITCH一样可行,并且可以避免代码中的“ goto”(尽管它可能会以相同的方式编译)。

You could use goto . I wouldn't, but it'll do what you want.

您当然可以使用回调来调用委托,但是我不确定如何在goto之外完成“执行不返回到调用位置”部分。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM