Note that there are some explanatory texts on larger screens.

plurals
  1. PODebugging a generated .NET assembly from within the application that generated it
    primarykey
    data
    text
    <p>The question in short: How can I debug the code generated during a debugging session on the generating program? (see code below)</p> <p>I am facing the following issue: I would like to debug into dynamically generated/compiled code from the application that generates it. I provided an oversimplified example to clarify it. This example doesn't need debugging! My real app generates many more lines and code that really justify debugging, believe me :-) I would like to know if there is a way to debug or put a breakpoint at <code>HelloWorld</code>. Stepping into the InvokeMethod call doesn't work. Maybe a solution involves code modification at the call sites to the generated assembly.</p> <p>I had a look at many questions already (<a href="https://stackoverflow.com/questions/1295807/debug-dynamically-loaded-assembly-in-visual-studio-net">Debug dynamically loaded assembly in Visual Studio .NET</a> for example) but none was helpful in solving the problem (if solvable at all?)</p> <p>I took code from <a href="http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=118" rel="nofollow noreferrer">http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=118</a> as a base and fixed the obsoleted calls. Beside this I generated the assembly on-the-fly in memory and the calls are working well. I generated explicitly an assembly with Debug information, what gives me hope: why would there be the option if debugging is not possible?</p> <pre><code>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); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload