Hi I am trying to find a way of executing code generated at runtime, without restarting the program.
My current workflow:
User uses my program to create some diagrams
User presses compile and C# code is generated from the users diagrams
The C# code is placed in a code file which exists in my project's hierarchy. The program is then restarted in order to use the newly generated code.
What I'm after though is changing Step 3, so I don't have to restart the program. Something like:
The C# code is run through the C# compiler which generates a DLL.
any errors are shown to the user
The DLL is then loaded into the program
Setup some links to the codes methods, so I can pass data to it and get output data
if this would work?
Look at the CSharpCodeProvider. It is intended for compiling a code in memory.
Related
I am trying to provide protection and encryption for some build artifacts that our code produces (The code is written in c++ and c# and we use visual studio) . The tool that I am using is a product called AxProtectorNet which belongs to Wibu-systems company. While doing the protection (using a batch file in which I passed the dlls and the protector tool location) for one dll i am getting the error saying "file protected\Ramin.dll is locked". I wonder what it means. Does it mean it's already protected? Or is it corrupted?
It's worth mentioning that we got the code from another company. So we don't know much about the details on the code.
There is a c# Application that compiles C# code stored in DB and creates a DLL in memory using roslyn compiler that the DLL is loaded in memory and an instance of the type is created from the following code
Activator.CreateInstance(type); // Just creates an instance of the type so that any public method or property can be accessed
The reason for having the code in DB is abit complex to explain here for the scope of the question but it is important to note there is no physical file where C# code is present.
Also please note the DLL is created at runtime by the application in release mode or debug mode depending if #ifdebug is true or not.
Now what we want is if the application is running in debug mode we should be able to debug the DLL created in runtime. Can experts over here help me to give pointers how to achieve it. Since access to code is there from DB we can in runtime create a temporary file in some temporary location if the application is running in debug mode but how do we let Visual studio know we want to link the specific DLL to this temporary C# file for the purpose of debugging. Please note the DLL is in memory in some concurrent bag and we arent creating PDB file too
Any pointers will be helpful We are using Visual studio 2022
This is possible in Visual Studio 2022!
Follow the guidance of emitting the portable PDB from this answer. Since you didn't show much of how you are compiling, I will assume it's pretty close to that and getting the source code emitted won't be a problem.
When the assembly is finally loaded into the program, VS2022 will automatically pick up the symbols. You can verify this by looking for a your named DLL in the Modules window. Ctrl+Alt+U or Tools>Windows>Modules. From the answer I posted, it will be randomly named.
When you have the program running, the external sources node (which is new with VS2022) will show that same random DLL name.
If you open the generated file, you should see your generated code. You are also able to place breakpoints in this file!
Not sure if it matters or not, but in my VS options I have disabled "Just My Code".
I'm not sure if this is possible with Visiual Studio but maybe you could have some luck with dnSpy:
https://github.com/dnSpy/dnSpy
I was able to debug a dll that was loaded by a program at runtime using:
System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromAssemblyPath(path);
so maybe it can help you too.
The task is that the contents of the dll file becomes known only during the execution of the main program, in which I get the necessary data and form a line or cs file. Then I need to create a dll from the code I wrote in the line (let's say it's correct and compiles without errors). Then I will dynamically connect this dll to the main program and use the function from there.
The main problem is in the dynamic creation of the dll file, I did not find a working option. I am working in WPF .NET 5.0, I tried CodeDOM but there was an error "not supported on this version" so I will be very grateful for any information on how to do it!
In Visual Studio when I "build" my project, does that mean that I "compiled" the source code to machine code?
If so, why not call it "Compile"?
As said by #pm100, Building does many things apart from compiling, the compiler at first compiles the code from C# to byte code (not to machine language in C# case). Here you get multiple pieces of compiled code, these pieces are not related to each other.
Here comes the role of linker, it links the multiple pieces (they are also called objects). Now the files knows how communicate and use the code from each other.
Now Visual Studio may do something else, like calling post-build hooks, copy the files to the output directory, etc.
I am trying to create an application can modify properties in IL to create a slightly different executable. E.g Client A runs app and a label on the WinForm label Reads "Client A:". Client B runs the app and Label Says "Client B". Easy I know using config files or resource files but thats not an option for this project. The Main program needs to be able to generate .exe file dynamically based on some form fields entered by user.
My solution was to create a standalone executable that contained all the elements which I needed to make dynamic. I then used ildasm to generate the IL and thought that I could use this IL and substitute tags for the elements i wanted to make dynamic. I could then replace those tags at runtime after user filled the form using regex etc.
The problem is, the if i re save the IL file generated by ILDASM as an exe and try to run it. I just launches console and does nothing.
Am I going about this the wrong way? I didnt want to delve into Reflection as the dynamic .exe is a really simple one and I thought reverse engineering IL with ildasm would be the quickest way.
You thoughts and pointers are much appreciated.
Tony
Is the executable generated on-site? Or do you generate the executable for a customer and then ship the result?
If you are the one generating the executable, you could put the customer-specific data/code in a separate DLL and embed it in your executable as a resource, and load it when the AppDomain.CurrentDomain.AssemblyResolve event occurs.
That way you can have version control over the customer-specific data.
When you say "re-save" the file created by ildasm and run it do you mean you are running all of the IL files through ilasm (the provided Microsoft IL compiler)? If not, you may want to try that. Do be very careful when performing your substitution as IL has some quite specific requirements and ilasm is not as forgiving as the higher level compilers like csc and vbc.
You can also use Mono.Cecil to modify assembly files, that way you don't have to ship IL to your customers.
If you are going to continue on the route of shipping the source and compiling it you can accomplish the same thing without involving IL by shipping the C# source and compiling it onsite since csc is included in the framework.