3

I am working on a project where there is a need to generate c# code in real time. The resulting code should be used as a new library in other projects. To explain clearly, Please look at PaintCode, a program like this is in my mind. which technologies do you think they have applied to produce code generation? I know we can use System.Reflection to generate types as mentioned here. But How can I grab user actions and generate method bodies dynamically. Does exist any standard solution to do this in .NET Or I have to make everything myself from scratch?

Senario

For example suppose we have implemented an environment that user can insert operations (blue square) and objects (orange circles) to it with drag and drop. In the following picture user wants to define an Add operation for two input objects a and b.


enter image description here Suppose a and b are objects of a predefined type "Box" and "Add" is a simple + operator. When user drags a line from A.length and B.length to the + square it means A.length + B.length. So when the user does this I need a way to store it and create a c# code like the following code block in real time. (It's an example for clarity please don't waste your time to analyze it):

using blabla.Box;

public static Box operator+ (Box a, Box b)
      {
         Box box = new Box();
         box.length = b.length + c.length;
         box.height = b.height + c.height;
         return box;
      }

All of this magic should happen real time and automatically.

The Question

If I save all user's activity in a list. How can I convert this object to a c# code? What should I do to find the dependencies and required namespaces in addition to my own. What can CodeDOM do for me? Do I need to build the codes line by line and manually? Or does exist any library or something like that which help me in this regard? Thanks for any suggestion.

Community
  • 1
  • 1
a.toraby
  • 3,232
  • 5
  • 41
  • 73
  • 1
    Have you checked out the CodeDOM at https://msdn.microsoft.com/en-us/library/650ax5cx(v=vs.110).aspx – racraman Feb 08 '15 at 07:57
  • 1
    Do you need to generate c# code (e.g. text) or msil instructions? – igorushi Feb 08 '15 at 08:14
  • Maybe Microsoft's new Roslyn compiler? https://github.com/dotnet/roslyn and do a Google search for ".net roslyn". – RenniePet Feb 08 '15 at 08:28
  • @Gosha The ideal goal is to generate c# code. But not required if msil generation is simpler than. Its enough if the generated code is applicable as a new library. (.dll for example). – a.toraby Feb 08 '15 at 08:29
  • possible duplicate of [Is it possible to dynamically compile and execute C# code fragments?](http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments) – igorushi Feb 08 '15 at 09:30
  • @a.toraby your extract question seems to be answered many times : http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments Once you discover what you can get from the frameworks, I sugest you to define in terms of interfaces what you will provide as an input and what output you wish to get. This will help to find and apply right solution. – igorushi Feb 08 '15 at 09:41
  • I updated my question and added more information. I hope It clarifies my problem. Thanks for your attention. – a.toraby Feb 08 '15 at 13:49

0 Answers0