I'm trying to make an application that has a few resources listed as static fields. I want to access those fields from my DLR scripts. However, I've noticed that the Dlr code seems to have different values for statics than the C# code.
Below is a small test application that demonstrates the problem.
using Microsoft.Scripting.Hosting;
using System;
namespace SegmentTest {
public static class Dlr {
public static int MyFakeGlobalValue = 7;
static readonly ScriptScope _scope;
static Dlr() {
_scope = IronRuby.Ruby.CreateEngine().CreateScope();
var init = _scope.Engine.CreateScriptSourceFromString(@"
require 'SegmentTest'
include SegmentTest
");
init.Execute(_scope);
}
public static dynamic Execute(string script) {
return _scope.Engine.Execute(script, _scope);
}
}
class Program {
static void Main(string[] args) {
Dlr.MyFakeGlobalValue = 12;
Console.WriteLine(Dlr.MyFakeGlobalValue);
Console.WriteLine(Dlr.Execute("Dlr.MyFakeGlobalValue"));
Console.ReadKey();
}
}
}
The first Console.WriteLine shows that MyFakeGlobalValue is 12. The second call shows that the value is 7. Apparently, changes to statics in my CLR classes are not in the same context as the DLR code is running.
In my real application, I have several classes that call Ruby code, and I want my Ruby code to be able to create those classes. However, I then end up with some sort of nested context problem where a Ruby script won't have access to variables created in an earlier context.
Is there a way to get the statics to be the same in both the application and the scripts?