短い質問:プログラムを生成中にデバッグセッション中に生成されたコードをどのようにデバッグできますか? (下のコードを参照してください)生成された.NETアセンブリを生成したアプリケーション内からデバッグする
私は次の問題に直面しています:それを生成するアプリケーションから動的に生成/コンパイルされたコードにデバッグしたいと思います。私はそれを明確にするための単純化された例を提供しました。この例ではデバッグは必要ありません。私の実際のアプリケーションは、デバッグを本当に正当化するより多くの行とコードを生成します:-)私はデバッグする方法があるかどうかを知りたいですか、またはブレークポイントをHelloWorld
に置くことができます。 InvokeMethod呼び出しにステップインしても機能しません。たぶん、ソリューションは、コールサイトで生成されたアセンブリへのコード変更を伴います。
私はベースとしてhttp://www.csharpfriends.com/Articles/getArticle.aspx?articleID=118からコードを取り、廃止された通話を固定しました。これに加えて、私はメモリ上でオンザフライでアセンブリを生成し、呼び出しはうまくいっています。私は明示的にデバッグ情報を持つアセンブリを生成しました。私に希望を与えるもの:デバッグが不可能な場合、なぜオプションがありますか?
using System;
using System.Text;
using System.IO;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
namespace DynamicAssembly
{
class CreateCompileExecute
{
[STAThread]
static void Main(string[] args)
{
// Creates a text file to store the new class
StringBuilder builder = new StringBuilder();
builder.AppendLine("using System;");
builder.AppendLine("namespace CSharpFriendsRocks");
builder.AppendLine("{");
builder.AppendLine("class CSharpFriends");
builder.AppendLine("{");
builder.AppendLine("public CSharpFriends() {" +
" Console.WriteLine(\"The CSharpFriends type is constructed\"); }");
builder.AppendLine("public void HelloWorld() {" +
" Console.WriteLine(\"Hello World - CSharpFriends.Com Rocks.\"); }");
builder.AppendLine("}");
builder.AppendLine("}");
// Create the C# compiler
CSharpCodeProvider csCompiler = new CSharpCodeProvider();
// input params for the compiler
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.OutputAssembly = "CSharpFriends.dll";
compilerParams.GenerateInMemory = true;
compilerParams.IncludeDebugInformation = true;
compilerParams.ReferencedAssemblies.Add("system.dll");
compilerParams.GenerateExecutable = false; // generate the DLL
// Run the compiler and build the assembly
CompilerResults results = csCompiler.CompileAssemblyFromSource(
compilerParams, builder.ToString());
// Load the generated assembly into the ApplicationDomain
Assembly asm = results.CompiledAssembly;
Type t = asm.GetType("CSharpFriendsRocks.CSharpFriends");
// BindingFlags enumeration specifies flags that control binding and
// the way in which the search for members and types is conducted by reflection.
// The following specifies the Access Control of the bound type
BindingFlags bflags = BindingFlags.DeclaredOnly | BindingFlags.Public
| BindingFlags.NonPublic | BindingFlags.Instance;
// Construct an instance of the type and invoke the member method
Object obj = t.InvokeMember("HelloWorld", bflags |
BindingFlags.CreateInstance, null, null, null);
// Call the method
t.InvokeMember("HelloWorld", bflags | BindingFlags.InvokeMethod,
null, obj, null);
}
}
}
あなたはhttp://stackoverflow.com/questions/321203/how-do-i-debug-il-code-generated-at-runtime-using-reflection-emitを見ましたか? –
私は実際にそれをILコードを視覚化しないようにデバッグしたいのですか、何か迷っていましたか? – jdehaan
それは可能ではないと答えたのですか? – jdehaan