大概的 API 設計如下:
RemoteExecutor.Invoke(() =>
{
// 在這裏編寫在新進程執行的委託代碼
});
要在 Main 函數裏面調用 RemoteExecutor.TryHandle 處理命令行,因為新進程裏面執行的邏輯本身就需要 Main 函數參與。標準預期寫法如下
if (RemoteExecutor.TryHandle(args))
{
return;
}
核心實現原理就是反射獲取委託所在的方法,通過方法反射調用而已
大家都知道,在 C# dotnet 裏面,委託是會被生成為獨立類型的。利用此原理,獲取委託所在的程序集、類型、方法名,將其作為命令行參數傳遞過去到新進程。在新進程裏面,讀取傳入的命令行參數,瞭解到當前應該反射執行哪個方法,從而執行到委託的邏輯
這個過程裏面,可以看到是給新進程去執行的,意味着過程中禁止任何捕獲字段,任何的委託捕獲對象都不能給傳遞到新進程裏面
以下是示例調用的全部代碼:
using System.Globalization;
using RemoteExecutors;
if (RemoteExecutor.TryHandle(args))
{
return;
}
RemoteExecutor.Invoke(() =>
{
// 寫個文件測試一下
var file = Path.Join(AppContext.BaseDirectory, "1.txt");
File.WriteAllText(file, DateTime.Now.ToString(CultureInfo.InvariantCulture));
});
Console.WriteLine("Hello, World!");
可以看到在這個寫法裏面,可以很方便將一個委託放在另一個進程去執行
本文提供的 RemoteExecutor 類的實現也非常簡單,大家看一下代碼就明白了原理
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<RemoteExecutorOption>(option =>
{
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";
[Option]
public required string AssemblyName { get; init; }
[Option]
public required string ClassName { get; init; }
[Option]
public required string MethodName { get; init; }
}
本文代碼放在 github 和 gitee 上,可以使用如下命令行拉取代碼。我整個代碼倉庫比較龐大,使用以下命令行可以進行部分拉取,拉取速度比較快
先創建一個空文件夾,接着使用命令行 cd 命令進入此空文件夾,在命令行裏面輸入以下代碼,即可獲取到本文的代碼
git init
git remote add origin https://gitee.com/lindexi/lindexi_gd.git
git pull origin a3a10076765bea695136442c8745d18b42d840f2
以上使用的是國內的 gitee 的源,如果 gitee 不能訪問,請替換為 github 的源。請在命令行繼續輸入以下代碼,將 gitee 源換成 github 源進行拉取代碼。如果依然拉取不到代碼,可以發郵件向我要代碼
git remote remove origin
git remote add origin https://github.com/lindexi/lindexi_gd.git
git pull origin a3a10076765bea695136442c8745d18b42d840f2
獲取代碼之後,進入 Workbench/DurwerjeguCalldemrereher 文件夾,即可獲取到源代碼