2

Is it possible writing code that generate a class, method, member at runtime using .NET (C#)?

For more details consider this scenario :

create a dynamic workflow program to enable user for creating his own process, activities, and writing dynamic SQL SPs, …, and collect all this stuff together then generate a classes, member variables , member functions , UIs, conditions, … dynamically at run-time ! in other word your own dynamic code factory framework !

Zakaria
  • 1,055
  • 12
  • 16
  • Take a look at the [TypeBuilder Class](http://msdn.microsoft.com/en-us/library/system.reflection.emit.typebuilder.aspx) – Alex Filipovici Jun 12 '13 at 06:48
  • There is an interesting project on CodeProject, Compiling C# code during runtime: http://www.codeproject.com/Articles/10324/Compiling-code-during-runtime - Maybe this will help – Smartis has left SO again Jun 12 '13 at 06:49
  • Please be aware that this easily can become very messy, and if you let your users introduce runtime code you're opening a potential security risk.. Especially when your application requires admin rights. – JustAnotherUserYouMayKnow Jun 12 '13 at 06:51

4 Answers4

5

Yes, there are various options for this:

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Quick answer: Yes!

For a detail of how to achieve this you would want to start your learning by looking at Reflection.

The next step would be looking for other resources on the internet and a quick search located this question on SO:

How to create a method at runtime using Reflection.emit

Dynamic Language Runtime may also be worth a look.

Community
  • 1
  • 1
Mr. Mr.
  • 4,257
  • 3
  • 27
  • 42
1

The short response is yes. You have to look and study the following technologies:

CodeDom

Windows Wordflow Foundation

If it is anyway useful can be discussed: One able to "dynamically" program a workflow in a so specific mode will probably prefer to write the code by hand himself.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
1

Another alternative of using strong types in this case, you may consider also using
Dynamic object to allow fully featured dynamic behaviour.

Could be more appropriate then strong typing generated at runtime, in this case.

Tigran
  • 61,654
  • 8
  • 86
  • 123