Reputation: 1001
As of this article I want to write a little C# scripting engine to work. I have tried this code:
System.IO.StreamReader sr = new System.IO.StreamReader("example.txt");
var prog = sr.ReadToEnd();
sr.Close();
ScriptOptions so = ScriptOptions.Default;
Console.WriteLine(CSharpScript.RunAsync(prog, so).Result.GetVariable("sum").Value);
Console.ReadKey();
And the file content of example.txt is:
System.IO.StreamReader sr = new System.IO.StreamReader("foo.txt");
string s = sr.ReadToEnd();
int sum = System.Convert.ToInt32(s);
Now I want to restrict the user to only basic functions of C# (using int, string, while, for) and some self defined functions but NOT the whole .NET library. But even if I don't include System.IO als reference the user can type System.IO.something and will get the right thing.
Is there a possibility to remove the access to "System" from the script?
I'm glad with any answer.
Upvotes: 5
Views: 1623
Reputation: 6420
You'll need to set up the ScriptingOptions
properly, meaning that a custom MetadataReferenceResolver
needs to be used instead of the default one, which is resolving the missing references automatically. I don't know if there is already a resolver that only resolves assemblies based on its parameters, but you can certainly implement yours. Check out the TestMetadataReferenceResolver
, which does something similar.
Update This won't work for things that are defined in mscorlib.
Upvotes: 1