Skip to content

Instantly share code, notes, and snippets.

@0x49D1
Last active November 22, 2023 04:34
Show Gist options
  • Save 0x49D1/23196eb99b4c1f089b2033b6191e84e8 to your computer and use it in GitHub Desktop.
Save 0x49D1/23196eb99b4c1f089b2033b6191e84e8 to your computer and use it in GitHub Desktop.
Sample using IronPython
public string PatchParameter(string parameter, int serviceid)
{
var engine = Python.CreateEngine(); // Extract Python language engine from their grasp
var scope = engine.CreateScope(); // Introduce Python namespace (scope)
var d = new Dictionary<string, object>
{
{ "serviceid", serviceid},
{ "parameter", parameter}
}; // Add some sample parameters. Notice that there is no need in specifically setting the object type, interpreter will do that part for us in the script properly with high probability
scope.SetVariable("params", d); // This will be the name of the dictionary in python script, initialized with previously created .NET Dictionary
ScriptSource source = engine.CreateScriptSourceFromFile("PATH_TO_PYTHON_SCRIPT_FILE"); // Load the script
object result = source.Execute(scope);
parameter = scope.GetVariable<string>("parameter"); // To get the finally set variable 'parameter' from the python script
return parameter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment