void ExecuteAndUnloadScript(string script) { using (CSScriptLibrary.AsmHelper helper = new CSScriptLibrary.AsmHelper(CSScriptLibrary.CSScript.Compile(Path.GetFullPath(script), null, true), null, true)) { helper.Invoke("*.Execute", this); } }
static void Main() { Assembly assembly = CSScript.LoadCode( @"using System; public class Calculator : ICalculator { public int Add(int a, int b) { return a + b; } public string Join(string a, string b) { return a + b; } }"); AsmHelper calc = new AsmHelper(assembly); object instance = calc.CreateObject("Calculator"); //calc.CreateObject("*") can be used too if assembly has only one class defined FastInvokeDelegate methodInvoker = calc.GetMethodInvoker("Calculator.Add", 0, 0); int numOfLoops = 1000000; TestReflection(numOfLoops, calc, instance); TestFastInvoking(numOfLoops, calc, instance); TestDelegates(numOfLoops, methodInvoker, instance); TestInterface(numOfLoops, instance); TestInterfaceAlignment(numOfLoops, instance); TestDynamic(numOfLoops, instance); TestCompiledCode(numOfLoops); TestCompiledDelegate(numOfLoops); //TestMethodDelegates(); }
//static Regex torlinkRegex = new Regex(@"http://www1\.newstorrentsspace\.info/freeone/file\.php/.{7}\.html"); static void Main(string[] args) { List<string> lsgurl = new List<string>(); if (Directory.Exists("spider")) Directory.CreateDirectory("spider"); Directory.GetFiles("spider","*.cs").ToList().ForEach(s => { dynamic script = new AsmHelper(CSScript.Load(s)).CreateObject("SpiderHost.script.spider"); lsgurl.AddRange(script.getUrls()); }); //Parallel.For(1, 2, (i => //{ // lsgurl.Add("http://opp.hegc1024.com/pw/thread.php?fid=5&page=" + i); //})); //Parallel.For(1,2, ( i=>{ // lsgurl.Add("http://opp.hegc1024.com/pw/thread.php?fid=22&page=" + i); //})); dowload_av(lsgurl.ToArray()); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Done!"); Console.ForegroundColor = ConsoleColor.White; Console.ReadLine(); }
static void Main() { CSScript.GlobalSettings.UseAlternativeCompiler = Path.GetFullPath("CSSCodeProvider.dll"); //compiler that understands JS/VB.NET/C++ and classless-C# var script = new AsmHelper(CSScript.Load("Hello.js")); script.Invoke("*.Main"); }
void Start() { CreateDocument(); var script = new AsmHelper(CSScript.Load("script.cs", null, true)); script.Invoke("*.Execute", this); }
/// <summary> /// Executes the script and adds it to the current app domain /// NOTE: This locks the assembly script file /// </summary> public void ExecuteScriptInternal(String script, Boolean random) { if (!File.Exists(script)) throw new FileNotFoundException(); String file = random ? Path.GetTempFileName() : null; AsmHelper helper = new AsmHelper(CSScript.Load(script, file, false, null)); helper.Invoke("*.Execute"); }
static void ExecuteAndUnloadScript(string script) { using (AsmHelper helper = new AsmHelper(CSScript.Compile(script), null, true)) { helper.Invoke("*.Execute"); } }
void ExecuteAndUnloadScript(string script) { using (AsmHelper helper = new AsmHelper(CSScript.Compile(script, Path.GetFullPath("ExternalAsm.dll")), null, true)) { helper.Invoke("*.Execute"); } }
static void Main() { Assembly assembly = CSScript.LoadCode( @"using System; public class Claculator : IClaculator { public void Sum(int a, int b) { Console.WriteLine(a + b); } public void Multiply(int a, int b) { Console.WriteLine(a * b); } }"); AsmHelper script = new AsmHelper(assembly); object instance = script.CreateObject("Claculator"); TypeUnsafeReflection(script, instance); TypeSafeInterface(instance); TypeSafeAlignedInterface(instance); TypeSafeDelegate(script, instance); TypeUnSafeDelegate(script, instance); }
private void CompileClasses(string code) { // FileInfo fi = new FileInfo( @"C:\src\xgc3\xgc3\Generated.cs"); // string code = File.ReadAllText(fi.FullName); m_helper = new AsmHelper(CSScriptLibrary.CSScript.LoadCode(code)); //, null, true)); //CreateInstance("test", "xgc3.Generated.Test"); }
/// <summary> /// Executes the script in a seperate appdomain and then unloads it /// NOTE: This is more suitable for one pass processes /// </summary> public void ExecuteScriptExternal(String script) { if (!File.Exists(script)) throw new FileNotFoundException(); using (AsmHelper helper = new AsmHelper(CSScript.Compile(script, null, true), null, true)) { helper.Invoke("*.Execute"); } }
static void CallStaticMethodOfTheOnlyClassAvailableInClasslessScript() { CSScript.GlobalSettings.UseAlternativeCompiler = Environment.ExpandEnvironmentVariables(@"%CSSCRIPT_DIR%\Lib\CSSCodeProvider.dll"); var helper = new AsmHelper(CSScript.Load("script1.ccs", null, true)); helper.Invoke("*.SayHello", "Hello World!"); }
static void CallStaticMethodOfSpecifiedClassWithNamespaceInClasslessScript() { CSScript.GlobalSettings.UseAlternativeCompiler = Environment.ExpandEnvironmentVariables(@"%CSSCRIPT_DIR%\Lib\CSSCodeProvider.dll"); var helper = new AsmHelper(CSScript.Load("script2.ccs", null, true)); helper.Invoke("MyNamespace.MyClass.SayHello", "Hello World!"); }
public ScriptingEngine( string code) { // FileInfo fi = new FileInfo( @"C:\src\xgc3\xgc3\Generated.cs"); // string code = File.ReadAllText(fi.FullName); m_helper = new AsmHelper(CSScript.LoadCode(code) ); //, null, true)); CreateInstance("test", "xgc3.Generated.Test"); }
static void TypeSafeDelegate(AsmHelper script, object instance) { FastInvokeDelegate sumInvoker = script.GetMethodInvoker("Claculator.Sum", 0, 0); //type unsafe delegate Action<int, int> Sum = delegate(int a, int b) { sumInvoker(instance, a, b); }; //type safe delegate Sum(1, 2); Sum(4, 7); }
static void TestWithAsmHelper() { //Note usage of CreateObject("Script") is also acceptable var script = new AsmHelper(CSScript.Load("HelloScript.cs")) .CreateObject("*") .AlignToInterface<IScript>(); script.Hello("Hi there..."); }
static void CallStaticMethod1() { //AsmHelper allows accessing scripts methods with wild card Assembly assembly = CSScript.LoadMethod( @"public static void SayHello(string greeting) { Console.WriteLine(greeting); }"); AsmHelper script = new AsmHelper(assembly); script.Invoke("*.SayHello", "Hello World!"); }
static void CallStaticMethod2() { //AsmHelper can generate "object FastInvokeDelegate(object instance, params object[] paramters)" //delegate which is more convenient to work with. FastInvokeDelegate SayHello = new AsmHelper(CSScript.LoadMethod( @"public static void SayHello(string greeting) { Console.WriteLine(greeting); }")) .GetMethodInvoker("*.SayHello", ""); SayHello(null, "Hello World!"); }
static void CallInstanceMethod2() { Assembly assembly = CSScript.LoadMethod( @"public void SayHello(string greeting) { Console.WriteLine(greeting); }"); AsmHelper script = new AsmHelper(assembly); object instance = script.CreateObject("*"); //wild card * can be used as the assembly contains only one class defined script.InvokeInst(instance, "*.SayHello", "Hello World!"); }
static void CallStaticMethodOfDefaultClassInClasslessScript() { //There is a script file to execute. //The script consist of the only method definition. //Autoclass is injected by CS-Script engine and user has no control //on class name, which is always Scripting.DynamicClass CSScript.GlobalSettings.UseAlternativeCompiler = Environment.ExpandEnvironmentVariables(@"%CSSCRIPT_DIR%\Lib\CSSCodeProvider.dll"); var helper = new AsmHelper(CSScript.Load("script1.ccs", null, true)); helper.Invoke("script1.Script.SayHello", "Hello World!"); }
static void Main() { var Sum = new AsmHelper(CSScript.LoadMethod( @"public static int Sum(int a, int b) { System.Diagnostics.Debugger.Break(); Host.Log(""Calculating sum...""); return a + b; }", null, true)) .GetStaticMethod(); int result = (int)Sum(1, 2); }
static void CallInstanceMethod1() { //all classless scripts belong to DynamicClass type generated by the CS-Script engine Assembly assembly = CSScript.LoadMethod( @"public void SayHello(string greeting) { Console.WriteLine(greeting); }"); AsmHelper script = new AsmHelper(assembly); object instance = script.CreateObject("Scripting.DynamicClass"); // or script.CreateObject("*") script.InvokeInst(instance, "Scripting.DynamicClass.SayHello", "Hello World!"); }
/** * Lädt das CS-Script interpretiert als FireSequence und führt * Sequence aus. * @returns String Nachricht */ public String LoadScript(String className) { String msg = ""; //Rückgabenachricht // Script laden und interpretieren var scriptAssembly = CSScript.Load("./Scripts/" + className + ".cs"); AsmHelper assemblyHelper = new AsmHelper(scriptAssembly); var fireSequence = (IFireSequence)assemblyHelper.CreateObject(className); msg += ExecuteFiringSequence(fireSequence); return msg; }
void Initialise() { InitializeComponent(); upBtn.Text = downBtn.Text = ""; if (Environment.Version.Major >= 2) { // the following code must be generated dynamically as ShellEx is guaranteed to be run //under CLR 1.1 as part of the installation sequence (config.cs) and 1.1 cannot handle //"this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView1_DrawNode);" treeViewExtension = new AsmHelper(CSScript.LoadCode(treeViewExtensionCode, null, false, this.GetType().Assembly.Location)); treeViewExtension.Invoke("*.Extend", treeView1); } }
static void CallStaticMethod3() { //AsmHelper can generate "object MethodDelegate(params object[] paramters)" //delegate which is more convenient to work with. //MethodDelegate is just specialized version of FastInvokeDelegate var SayHello = new AsmHelper(CSScript.LoadMethod( @"public static void SayHello(string greeting) { Console.WriteLine(greeting); }")) .GetStaticMethod("*.SayHello", ""); SayHello("Hello World!"); }
static void Run1() { var code = @"public static void Hello(string greeting) { SayHello(greeting); } static void SayHello(string greeting) { Console.WriteLine(greeting); }"; var script = new AsmHelper(CSScript.LoadMethod(code)); // /call first Hello method found with Reflection based MethodInvoker script.Invoke("*.Hello", "Hello World!"); }
public static void Main(string[] args) { try { // Create a new, empty permission set so we don't mistakenly grant some permission we don't want PermissionSet permissionSet = new PermissionSet(PermissionState.None); // Set the permissions that you will allow, in this case we only want to allow execution of code permissionSet.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution)); // Make sure we have the permissions currently permissionSet.Demand(); // Create the security policy level for this application domain PolicyLevel policyLevel = PolicyLevel.CreateAppDomainLevel(); // Give the policy level's root code group a new policy statement based on the new permission set. policyLevel.RootCodeGroup.PolicyStatement = new PolicyStatement(permissionSet); CSScript.GlobalSettings.AddSearchDir(Environment.CurrentDirectory); File.Copy("Danger.cs", "Danger1.cs", true); var script = new AsmHelper(CSScript.Load("Danger.cs")); // Update the application domain's policy now AppDomain.CurrentDomain.SetAppDomainPolicy(policyLevel); var script1 = new AsmHelper(CSScript.Load("Danger1.cs")); Console.WriteLine(); Console.WriteLine("Access local file from host application assembly..."); using (FileStream f = File.Open("somefile.txt", FileMode.OpenOrCreate)) //OK because executing assembly was loaded before the new policy set Console.WriteLine(" Ok"); Console.WriteLine(); Console.WriteLine("Access local file from Script assembly (before security policy set)..."); script.Invoke("*.SayHello"); //OK because executing assembly was loaded before the new policy set Console.WriteLine(); Console.WriteLine("Access local file from Script assembly (after security policy set)...\n"); script1.Invoke("*.SayHello"); //ERROR because executing assembly was loaded after the new policy set Console.WriteLine("The end..."); } catch (Exception e) { Console.WriteLine(); Console.WriteLine(e.Message); Console.WriteLine(); } }
static void Main() { CSScript.GlobalSettings = new Settings(); //create default settings instance instead of using the one initialized from CS-Script installation (if present) var Sum = new AsmHelper(CSScript.LoadMethod( @"public static int Sum(int a, int b) { Host.Log(""Calculating sum...""); return a + b; }")) .GetStaticMethod(); int result = (int)Sum(1, 2); Log("Result: " + result); }
public static void ResetGrabber() { if (_asmHelper != null) { _asmHelper.Dispose(); _asmHelper = null; } if (_movieImagesGrabber != null) { _movieImagesGrabber.SafeDispose(); _movieImagesGrabber = null; } _movieImagesGrabberLoaded = false; }
private void validateToolStripMenuItem_Click(object sender, EventArgs e) { textBox_error.Text = ""; try { Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; CSScript.AssemblyResolvingEnabled = true; AsmHelper script = new AsmHelper(CSScriptLibrary.CSScript.LoadCode(textBox_code.Text, Path.GetTempFileName(), true)); MessageBox.Show("No error"); } catch (Exception ex) { textBox_error.Text = ex.Message; } }
void ExecuteScript(string script) { CSScriptLibrary.AsmHelper helper = new CSScriptLibrary.AsmHelper(CSScriptLibrary.CSScript.Load(script, null, true)); helper.Invoke("*.Execute", this); }