[.net] Easy Scripting in .NET

Started by
72 comments, last by ppp_vcf 16 years, 5 months ago
My book shows how to use the CodeDom compiler to automate an MVC object model; essentially allowing the ability to write scripts that automate your applications (macros, things like that).

You can also use CodeDom to spit out assemblies you can link to at a later date. For example, all my business objects are represented by XSD and WSDL files, and I wrote a custom compiler that takes my specs and builds business entity objects with them. This allows me to take specifications, build a schema, and auto generate all the code with the special constraints, assert checks, logging, etc..

~Graham
Advertisement
I was just wondering how i would link in the use of a static variable within the program.

For example...

public class Test
{ public static string[] TestStringArray; }

I would need this to access and modify client information based on location and other attributes from the origonal application, acessed by the script.
After some playing with this (very useful) code, I still haven't figured out how to specify arguments for the called function. All my attempts have lead me to NullReferenceExceptions. Any idea how I would do this? Using temporary files doesn't look like a good solution to me, but it's the only thing I've been able to come up with for now.
Hello everyone!

I am trying to get a script engine running from my main program.
The problem i encountered is, that i have to access a member of the main class from the script.
I think i would be better if i explain my program a bit:

I have a main winform, which gathers data from a serial port (the data is provided by a microcontroller).
i would like the script to access the gathered data (which is stored in a variable of the CControl class) and access the main form's "Send()" method, so the script could read data and send commands to the microcontroller.

Is it possible to access the member variables of the Main Thread from a script? How could it be done?
Thanks
Nukleon
Quote:Original post by Nukleon
... I am trying to get a script engine running from my main program.
The problem i encountered is, that i have to access a member of the main class from the script.
I think i would be better if i explain my program a bit:
...
Is it possible to access the member variables of the Main Thread from a script? How could it be done? ...


Assuming that what you mean is what I think it is ;), I think you can do this by creating a library (at least that's what I'm doing) that contains the information from that class. Like this:

Script:
using MyLib;class X{    void Y()    {       MyLib.ClassName.Yatta;    }}


MyLib:
class ClassName{    // define your stuff here}


And then make the main program Invoke() the 'script', using MyLib as a reference. In order to do that correctly, you need to make sure it's in the same path, or specify the correct path of the DLL. Of course the above is only pseudo-code, but it demonstrates what I mean. Combined with the information above, I think you'll be able to set up something useful.

I hope to have been of some help to you.
Just curious, is everyone doing this "compile the script at runtime" approach? Are there any major advantages over just loading already compiled assemblies?

Whenever I make a plugin system I have it load assemblies instead of scripts, so that they don't have to be compiled. The main advantages I see are that 1) compilation is a pre-process and 2) any .Net language can be used for scripting, whether the actual end-user has the compiler for it or not. Following from 2, I also think it is more portable since it doesn't rely on the Microsoft namespace.

Is there some problem with this that everyone has?
Turring Machines are better than C++ any day ^_~
Quote:Original post by intrest86
Just curious, is everyone doing this "compile the script at runtime" approach? Are there any major advantages over just loading already compiled assemblies?
These are two completely different designs. On one hand, you're allowing the non-developer to hack away at the scripts, and on the other hand you're allowing the developer to hack away at the API.
Rob Loach [Website] [Projects] [Contact]
Quote:Original post by backtrace
After some playing with this (very useful) code, I still haven't figured out how to specify arguments for the called function. All my attempts have lead me to NullReferenceExceptions. Any idea how I would do this? Using temporary files doesn't look like a good solution to me, but it's the only thing I've been able to come up with for now.


Quote: Original post by foubar
I was just wondering how i would link in the use of a static variable within the program.

For example...

public class Test
{ public static string[] TestStringArray; }

I would need this to access and modify client information based on location and other attributes from the origonal application, acessed by the script.


I have a VB answer for both of you. I am assuming that it would also work in C++ and C#, but as today was the first time that I used CodeDOM and was using VB, I will use it.

The key is to instantiate your class using VB. I did something like this:
Dim equ as objectequ = res.CompiledAssembly.CreateInstance("clsEquation")Y1 = equ.Y1(X)


I was writing a tool to graph functions entered by the user and was using CodeDOM to parse and execute the functions entered by the user. By creating an instance of the class, I was able to call the function normally. As the functions will only be entered by the end user, I do not need to worry about sandboxing, and I'm not sure if this would work if sandboxed. (As I said, I'm new to this.)

Hope this helps.
Quote:Original post by backtrace
After some playing with this (very useful) code, I still haven't figured out how to specify arguments for the called function. All my attempts have lead me to NullReferenceExceptions. Any idea how I would do this? Using temporary files doesn't look like a good solution to me, but it's the only thing I've been able to come up with for now.


Way back in the origional post there was the line:
results.CompiledAssembly.GetType( "Script").GetMethod( "Main" ).Invoke( null, null );

The Second parameter to Invoke is either Null for a method that has no parameters, or an array of objects for a method that requires parameters. The size of the array should match the number of parameters in the method that is about to be called.
Thanks for pointing this out, OP. Very handy info to have.
hippopotomonstrosesquippedaliophobia- the fear of big words

This topic is closed to new replies.

Advertisement