为人民解决问题 發表於 2026-2-23 09:33:00

dotnet 在新进程执行某段委托的方法

<p>大概的 API 设计如下:</p>
<pre><code class="language-csharp">RemoteExecutor.Invoke(() =&gt;
{
    // 在这里编写在新进程执行的委托代码
});
</code></pre>
<p>要在 Main 函数里面调用 <code>RemoteExecutor.TryHandle</code> 处理命令行,因为新进程里面执行的逻辑本身就需要 Main 函数参与。标准预期写法如下</p>
<pre><code class="language-csharp">if (RemoteExecutor.TryHandle(args))
{
    return;
}
</code></pre>
<p>核心实现原理就是反射获取委托所在的方法,通过方法反射调用而已</p>
<p>大家都知道,在 C# dotnet 里面,委托是会被生成为独立类型的。利用此原理,获取委托所在的程序集、类型、方法名,将其作为命令行参数传递过去到新进程。在新进程里面,读取传入的命令行参数,了解到当前应该反射执行哪个方法,从而执行到委托的逻辑</p>
<p>这个过程里面,可以看到是给新进程去执行的,意味着过程中禁止任何捕获字段,任何的委托捕获对象都不能给传递到新进程里面</p>
<p>以下是示例调用的全部代码:</p>
<pre><code class="language-csharp">using System.Globalization;
using RemoteExecutors;

if (RemoteExecutor.TryHandle(args))
{
    return;
}

RemoteExecutor.Invoke(() =&gt;
{
    // 写个文件测试一下
    var file = Path.Join(AppContext.BaseDirectory, "1.txt");
    File.WriteAllText(file, DateTime.Now.ToString(CultureInfo.InvariantCulture));
});

Console.WriteLine("Hello, World!");
</code></pre>
<p>可以看到在这个写法里面,可以很方便将一个委托放在另一个进程去执行</p>
<p>本文提供的 RemoteExecutor 类的实现也非常简单,大家看一下代码就明白了原理</p>
<pre><code class="language-csharp">public static class RemoteExecutor
{
    public static void Invoke(Action action)
    {
      var method = action.Method;

      Type? type = method.DeclaringType;

      if (type is null)
      {
            throw new ArgumentException();
      }

      TypeInfo typeInfo = IntrospectionExtensions.GetTypeInfo(type);

      var methodName = method.Name;
      var className = typeInfo.FullName!;
      var assemblyFullName = typeInfo.Assembly.FullName!;

      string[] commandLineArgs =
            [
                RemoteExecutorOption.CommandName,
                "--AssemblyName", assemblyFullName,
                "--ClassName", className,
                "--MethodName", methodName,
            ];

      var processPath = Environment.ProcessPath;
      if (processPath is null)
      {
            throw new InvalidOperationException();
      }

      var process = Process.Start(processPath,commandLineArgs);
      process.WaitForExit();
    }

    public static bool TryHandle(string[] commandLineArgs)
    {
      var index = commandLineArgs.IndexOf(RemoteExecutorOption.CommandName);
      if (index == -1)
      {
            return false;
      }

      var optionCommandLineArgs = commandLineArgs.Skip(index+1).ToList();
      var result = CommandLine.Parse(optionCommandLineArgs)
            .AddHandler&lt;RemoteExecutorOption&gt;(option =&gt;
            {
                var assemblyName = option.AssemblyName;
                var className = option.ClassName;
                var methodName = option.MethodName;

                var assembly = Assembly.Load(assemblyName);
                var classType = assembly.GetType(className)!;

                var methodInfo = classType.GetTypeInfo().GetDeclaredMethod(methodName)!;
                object? instance = null;
                if (!methodInfo.IsStatic)
                {
                  instance = Activator.CreateInstance(classType);
                }
                object? result = methodInfo.Invoke(instance, null);
                _ = result;
            })
            .Run();

      _ = result;

      return true;
    }
}

internal class RemoteExecutorOption
{
    public const string CommandName = "RemoteExecutor_F6679170-3719-49AB-9936-7CAB5AB6294D";

   
    public required string AssemblyName { get; init; }

   
    public required string ClassName { get; init; }

   
    public required string MethodName { get; init; }
}
</code></pre>
<p>本文代码放在 github 和 gitee 上,可以使用如下命令行拉取代码。我整个代码仓库比较庞大,使用以下命令行可以进行部分拉取,拉取速度比较快</p>
<p>先创建一个空文件夹,接着使用命令行 cd 命令进入此空文件夹,在命令行里面输入以下代码,即可获取到本文的代码</p>
<pre><code>git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin a3a10076765bea695136442c8745d18b42d840f2
</code></pre>
<p>以上使用的是国内的 gitee 的源,如果 gitee 不能访问,请替换为 github 的源。请在命令行继续输入以下代码,将 gitee 源换成 github 源进行拉取代码。如果依然拉取不到代码,可以发邮件向我要代码</p>
<pre><code>git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
git pull origin a3a10076765bea695136442c8745d18b42d840f2
</code></pre>
<p>获取代码之后,进入 Workbench/DurwerjeguCalldemrereher 文件夹,即可获取到源代码</p>


</div>
<div id="MySignature" role="contentinfo">
    <p>博客园博客只做备份,博客发布就不再更新,如果想看最新博客,请访问 https://blog.lindexi.com/</p>

<p>如图片看不见,请在浏览器开启不安全http内容兼容</p>

<img alt="知识共享许可协议" style="border-width: 0" src="https://licensebuttons.net/l/by-nc-sa/4.0/88x31.png"><br>本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名[林德熙](https://www.cnblogs.com/lindexi)(包含链接:https://www.cnblogs.com/lindexi ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我[联系](mailto:lindexi_gd@163.com)。<br><br>
来源:https://www.cnblogs.com/lindexi/p/19630685
頁: [1]
查看完整版本: dotnet 在新进程执行某段委托的方法