a thoughtful web.
Good ideas and conversation. No ads, no tracking.   Login or Take a Tour!
comment
rob05c  ·  3535 days ago  ·  link  ·    ·  parent  ·  post: Dynamically Declaring Functions and Methods

C# has lambdas which can be 'dynamically declared.'

  delegate int del(int i);
  static void Main(string[] args)
  {
      del myDelegate = x => x * x;
      int j = myDelegate(5); //j = 25
  }
You can also pass around the lambda/delegate to other functions, for them to call it.

    define parts of the function once in the database assemble it as a string at run time and execute it on the fly

That is technically possible in C#. But I don't recommend it. It's very dangerous. What would happen if your database somehow got "Directory.Delete("C:\Windows\system32")" in it?

Alternatively, consider adding a scripting language to your program (I recommend Lua). That would let you store scripts as strings in the database, and execute them in a controlled environment. You can ensure scripts don't have the ability to do bad things like delete files, and that they only have access to the C# functions you expose.

    "Does this have anything to do with the 'enlightenment' that I see all over the place when Lisp developers talk about loving their language so much?"

Yes. LISP does what you want fantastically. Everything you're asking for: homoiconicity, lambdas, first-class functions, partial application. But that is a deep rabbit hole. I definitely recommend diving down it someday. But for now, to actually accomplish your project, I'd stick with C#, and either embed a scripting language or use C# lambdas (possibly both).