none
I have VB Code in an external TXT file that I need executing from a main compiled program RRS feed

  • Question

  • The scenario is this :

    There are a number of templates that I open with sendkeys. Making the sendkey procedure inside a compiled program means I would have to compile the main program everytime we add a new template or change how the sendkeys work.

    Is there a way of having the txt file contain the vb script and dynamically using that code at runtime. Is seems to be able to be done in C# but I cannot make head or tail of the code. 

    Regards

    Tim / Kraken17

    Thursday, September 6, 2018 8:19 AM

All replies

  • When you say "seems to be able to be done in C#", can you point to a code example that makes you believe this?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, September 6, 2018 11:26 AM
    Moderator
  • There is the System.Reflection.Emit Namespace which can basically do what is asked but if you look at the samples it is not just plug in some text and away you go. 

    Lloyd Sheen

    Thursday, September 6, 2018 3:42 PM
  • hello, 

    also you can use Roslyn Api: Syntax Transformation , 

    and here is a code to compile "transform" using C# "from the documentation":

    String programPath = @"..\..\Program.cs";
    String programText = File.ReadAllText(programPath);
    SyntaxTree programTree =
                   CSharpSyntaxTree.ParseText(programText)
                                   .WithFilePath(programPath);
    
    String rewriterPath = @".\..\TypeInferenceRewriter.cs";
    String rewriterText = File.ReadAllText(rewriterPath);
    SyntaxTree rewriterTree =
                   CSharpSyntaxTree.ParseText(rewriterText)
                                   .WithFilePath(rewriterPath);
    
    SyntaxTree[] sourceTrees = { programTree, rewriterTree };
    
    MetadataReference mscorlib =
            MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
    MetadataReference codeAnalysis =
            MetadataReference.CreateFromFile(typeof(SyntaxTree).Assembly.Location);
    MetadataReference csharpCodeAnalysis =
            MetadataReference.CreateFromFile(typeof(CSharpSyntaxTree).Assembly.Location);
    
    MetadataReference[] references = { mscorlib, codeAnalysis, csharpCodeAnalysis };
    
    return CSharpCompilation.Create("TransformationCS",
        sourceTrees, 
        references, 
        new CSharpCompilationOptions(OutputKind.ConsoleApplication));

    but why not to add a class library represented by an abstraction ISendKeysHandler, and implement your logic on concrete object implementing the interface, and then load it through an IoC "for example Unity, or MEF", , and  each time you change the handler logic all you have to do is change the registration in the config file.

    also another solution is to have a spec json file, containing definitions about the logic to use, and then in your app you should have factory that generate handlers according to the specs in the json.

    Good coding;

    Thursday, September 6, 2018 5:14 PM