Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a function dynamically at run-time

It probably isn't even possible to do this, but I will ask anyway. Is it possible to create a function that receives a string and then uses it as a right side argument for the goes to operator (=>) used in lambda?

Actually, what I want to do is to be able to redefine an specific method of a specific class during runtime. I want to be write down a function with the program running and attaching it to a delegate. Is it possible?

like image 225
Leahn Novash Avatar asked Jul 03 '09 16:07

Leahn Novash


People also ask

How to create a function dynamically in Python?

Method 1: exec() It's the perfect way to dynamically create a function in Python! ? Python's built-in exec() executes the Python code you pass as a string or executable object argument. This is called dynamic execution because, in contrast to normal static Python code, you can generate code and execute it at runtime.

How do you call a function dynamically in Python?

To do this in Python you have to access the global namespace. Python makes you do this explicitly where PHP is implicit. In this example we use the globals() function to access the global namespace. globals() returns a dictionary that includes area as a key and the value is a reference to the area() function.

What is dynamic function example?

Dynamic function is a way of dynamically invoking a function call. The compiler will have limited knowledge of what you are up to so you will get run time errors if you don't use correct inputs and outputs. One example that runs different functions depending on user input: DEFINE VARIABLE iFunc AS INTEGER NO-UNDO.


1 Answers

You have several ways how to do it:

  • dynamically create lambda expression (look at Dynamic LINQ: Part 1)
  • dynamically create CodeDom model and compile it at run-time (look at CodeDom.Compiler namespace
  • dynamically create C#/VB.NET source code and compile it at run-time (look at CSharpCodeProvider and VBCodeProvider classes )
  • dynamically create object model, which can do same things like the method (look at Strategy Design Pattern
like image 148
TcKs Avatar answered Sep 28 '22 12:09

TcKs