Dynamic creation and execution of code

  • Thread starter Thread starter JC
  • Start date Start date
J

JC

Hello,

I'm looking for examples of how to make dynamic creation
of code and dynamic execution of the same code. I want to
do some code dynamically and then I want to use it to get
some results. Then I want to store the C# code in a
database.
I found an example explaining how to do a sum of 100
numbers. What I didn't understand was the way to
generalize it's use.
Any solutions fot this ?

Thanks.
 
I'm looking for examples of how to make dynamic creation
of code and dynamic execution of the same code. I want to
do some code dynamically and then I want to use it to get
some results. Then I want to store the C# code in a
database.
I found an example explaining how to do a sum of 100
numbers. What I didn't understand was the way to
generalize it's use.
Any solutions fot this ?

after the source is compiled to memory, you can use reflection to analyze
the compiled assembly and use Delegate.CreateDelegate method to dynamically
create a delegate from compiled code.
then, using the delegate, you can use the code in the runtime. the delegate
can have any prototype and the Delegate.CreateDelegate method will check if
you create a delegate from proper method from the compiled code.
regards, Wiktor
 
JC,

In addition to what Wiktor said, I think that this is a bad idea, to try
and dynamically create code and then execute it (it's also a pain). If you
are going to insist on using dynamically generated code, then you probably
should at least define an interface which can always be bound to by the
caller, which the dynamically generated code must implement. This will make
it easer to access the code.

Hope this helps.
 
Back
Top