This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async void RunMacro() | |
{ | |
if(userScript == null) | |
MessageBox.Show("You must first compile the script."); | |
try | |
{ | |
ScriptState result = await userScript.RunAsync(); | |
} | |
catch(Exception e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void ValidatePropertyDeclarationSyntax(PropertyDeclarationSyntax declarationSyntax) | |
{ | |
IPropertySymbol symbol = semanticModel.GetDeclaredSymbol(declarationSyntax) as IPropertySymbol; | |
if (symbol.IsStatic) | |
{ | |
errors.Add((ValidationErrorType.Illegal_Feature, "Static properties are not allowed")); | |
return; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private bool IsIllegalAssemblyName(string assemblyName) | |
{ | |
return assemblyName switch | |
{ | |
"System.Console" => true, | |
"System.Diagnostics.Debug" => true, | |
"System.IO" => true, | |
"System.IO.FileSystem" => true, | |
"System.IO.FileSystem.Primitives" => true, | |
"System.Reflection" => true, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foreach(UsingDirectiveSyntax usingDirective in syntaxRootNode.Usings) | |
{ | |
ISymbol symbol = semanticModel.GetSymbolInfo(usingDirective.Name).Symbol; | |
string name = symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); | |
if(name == "System.IO") | |
throw new Exception("whoa!!, you can't be using that here!"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Compilation compilation; | |
private SemanticModel semanticModel; | |
private CompilationUnitSyntax syntaxRootNode; | |
// Validation Class Constructor | |
public ScriptValidator(Script script) | |
{ | |
compilation = script.GetCompilation(); | |
SyntaxTree syntaxTree = compilation.SyntaxTrees.First(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Script userScript; | |
private void CompileScript() | |
{ | |
UpdateSource(); //Handles creating a copy of the script which includes variables the user never sees. | |
string text = csSource.Text; //csSource is an object from the UI provider | |
userScript = CSharpScript.Create(text, options: userScriptOptions); | |
userScript.Compile(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static Script script; | |
public static async Task Main(string[] args) | |
{ | |
var userScriptPath = Path.Combine(Environment.CurrentDirectory, "scriptInput.txt"); | |
var fileContents = File.ReadAllText(userScriptPath); | |
script = CSharpScript.Create(fileContents, ScriptOptions.Default); | |
for (int i = 0; i < 15; i++) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
System.TimeSpan sinceMidnight = System.DateTime.Now - System.DateTime.Today; | |
var seconds = sinceMidnight.TotalSeconds; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static async Task Main(string[] args) | |
{ | |
var userScriptPath = Path.Combine(Environment.CurrentDirectory, "scriptInput.txt"); | |
var fileContents = File.ReadAllText(userScriptPath); | |
Script script = CSharpScript.Create(fileContents, ScriptOptions.Default); | |
var result = await script.RunAsync(); | |
Console.WriteLine($"Seconds in a week: {result.GetVariable("seconds").Value}"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.CodeAnalysis.CSharp.Scripting; |
NewerOlder