2

I don't think is possible but I just wanted to check if someone knew better. Basically, I don't want to store a sub-routine in the code itself but in a text file and then, I guess, import that and have it run as if it were a sub-routine.

So, the file would look like this:

Private Sub AutoRunsWinsockProvidersTranslation()

    AutoRuns.AutoRunsWinsockProviders(Me)
    AutoRuns.AutoRunsWriteData(True, "Winsock Providers")

End Sub

And it would be saved as, say, AutoRunsWinsock.txt. I want to know if I could import that somehow at run-time and then reference that sub-routine name or not. I suspect the answer is no but want to check.

The reason for this, by the way, is that then I could write a lot of txt files for different bits I'd want out of my other code without having to include them all before publishing the app.

shakydd
  • 61
  • 1
  • 8
  • In case you mean something like [Creating a function dynamically at run-time](https://stackoverflow.com/q/1080034/10216583) and [How to: Define and Execute Dynamic Methods](https://learn.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/how-to-define-and-execute-dynamic-methods) –  Jun 22 '20 at 17:00
  • 1
    The second article looks like voodoo but the first is interesting. In short, I don't know if it answers my question but particularly interested initially in looking further into System.Reflection.Emit. Thanks a lot for the pointer, I'll see what I can glean (googling some VB.NET examples of usage) – shakydd Jun 22 '20 at 17:07
  • 3
    Why do you want to do this in the first place? There are obviously valid reasons to compile code at run time but they are pretty rare. Many times, when people ask questions like this, they are trying to solve the wrong problem. The explanation you provide is very vague and doesn't really seem to justify what you're asking for. – jmcilhinney Jun 22 '20 at 17:23
  • Another option is the COM VBScript control. You can add objects to the control and then have it execute code from a string using `Eval()` or `Run()`. Examples: [here](https://stackoverflow.com/questions/45173940/how-to-evaluate-a-string-containing-references-to-windows-forms-controls-at-runt) and [here](https://www.experts-exchange.com/questions/22437865/Scripting-help.html#a18684842). Here is a [series of articles on MSDN](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-6.0/aa227413(v=vs.60)?redirectedfrom=MSDN). – Idle_Mind Jun 22 '20 at 17:25
  • This sounds like a major security nightmare though... – Idle_Mind Jun 22 '20 at 17:26
  • Thank you @Idle_Mind, I will check those out. Agreed it could be a major security problem. – shakydd Jun 22 '20 at 17:51
  • 1
    @jmcilhinney - Hi John - I possibly am trying to solve the wrong problem but there's only so many characters to type to not make it sound vague sometimes. At the risk of sounding vague again, I have a lot of code in a type library ready to be manipulated by a simple calls to a function, some of which can be manipulated by different parameters. I want to be able to write those on the fly and run accordingly. I also want to be able to publish new ones to different machines in my environment. A commercial example is Tanium sensors, which can be written and read by the engine, not embedded. – shakydd Jun 22 '20 at 17:56
  • 4
    You can load whole libraries at runtime, then use Reflection to look inside the libraries and find the methods you want. This will allow you to 'publish new ones to different machines in my environment'. I suggest looking into the concept and design patterns around Reflection before going down the live scripting path. For example, make a simple interface. Then in the DLLs you load at runtime, make your classes that contain all these methods implement the interface. You can reflect on any number of DLLs and find all types implementing the interface. – Sean Skelly Jun 22 '20 at 18:59

0 Answers0