これが最善の方法かどうかはわかりませんが、 Mono Cecilを使用してアプリケーションを書き直して、次のようにすることができます。
ModuleDefinition module = ModuleDefinition.ReadModule(fileName);
var entryPoint = module.EntryPoint.Body;
var corlib = module.TypeSystem.Corlib;
var exceptionTypeReference = new TypeReference(
"System", "Exception", null, corlib, false);
exceptionTypeReference = module.Import(exceptionTypeReference);
var entryPointIL = entryPoint.GetILProcessor();
var objectTypeReference = new TypeReference(
"System", "Object", null, corlib, false);
objectTypeReference = module.Import(objectTypeReference);
var writeLineMethod =
new MethodReference(
"WriteLine",
new TypeReference("System", "Void", null, corlib, true),
new TypeReference("System", "Console", null, corlib, false))
{ Parameters = { new ParameterDefinition(objectTypeReference) } };
writeLineMethod = module.Import(writeLineMethod);
var callWriteLine = entryPointIL.Create(OpCodes.Call, writeLineMethod);
entryPointIL.Append(callWriteLine);
entryPointIL.Emit(OpCodes.Br, entryPoint.Instructions.First());
var exceptionHandler = new ExceptionHandler(ExceptionHandlerType.Catch)
{
CatchType = exceptionTypeReference,
TryStart = entryPoint.Instructions.First(),
TryEnd = callWriteLine,
HandlerStart = callWriteLine,
HandlerEnd = entryPoint.Instructions.Last()
};
entryPoint.ExceptionHandlers.Add(exceptionHandler);
module.Write(fileName);
このコードが行うことは、アプリケーションのエントリ ポイント (通常はMain()
メソッド) を取得して、次のように書き直すことです。
static void Main()
{
// some code
}
これに:
static void Main()
{
while (true)
{
try
{
// some code
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
編集:
メソッドと包含クラスが public である場合Main()
、より簡単な方法があります。もう一方を参照として持つアプリケーションを作成し、そのMain()
. このようなもの:
class Program
{
void Main()
{
while (true)
{
try
{
OtherApplication.Program.Main();
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
}