I have a WPF application. For the purpose of this question, let's say it's a simple Window with a button. When I click on that button, I would like a Python script to be executed. Therefore, I went looking around and found out that I can run Python scripts using IronPython. Part1 works well, it runs the python scripts. From what I've gathered from looking around the web, Part2 is what I should do if I want to call a specific method.
private void btnWhatever_Click(object sender, RoutedEventArgs e)
{
//Basic engine to run python script. - Part1
ScriptEngine engine = Python.CreateEngine();
string pythonScriptPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
ScriptSource source = engine.CreateScriptSourceFromFile(pythonScriptPath + "/python.py");
ScriptScope scope = engine.CreateScope();
source.Execute(scope);
//Part2
Object myclass = engine.Operations.Invoke(scope.GetVariable("pythonScriptClass"));
object[] parameters = new object[] { "Hi",3 };
engine.Operations.InvokeMember(myclass, "theMethod", parameters);
}
The problem is, I kept getting 'Microsoft.Scripting.ArgumentTypeException' happened in Microsoft.Dynamic.dll : theMethod() takes exactly 2 arguments (3 given).
I understand from that error that I'm giving 3 arguments instead of 2 but I can't call a specific method another way from what I found out. I'm pretty new to IronPython and Python in general but here is a script example :
class pythonScriptClass:
def swapText(text, number):
return text[number:] + text[:number]
def getLetterIndex(letter, text):
for k in range(len(text)):
if (letter== text[k]):
return k
return -1
def theMethod(text , number):
result= swapText("textToBeSwaped", number)
toBeReturned = ""
for letter in text:
if letter in "abcdefghijklmnopqrstuvwxyz":
toBeReturned = toBeReturned + result[getLetterIndex(letter, result)]
return toBeReturned
My ultimate goal for the moment is to get this to work and therefore be able to call theMethod() from the Python script and get the returned value using C# - IronPython.
I have tried other methods such as : scope.SetVariable("key","value"); but I got the same error.
As for python member method, the first argument is self.
class pythonScriptClass:
def theMethod(self, text, number):
# and call self.swapText(...)
This is why the number of arguments went wrong.
Related
I tried to use Matlab function from my .net project
function [HR] = getHR(signal)
Fs = 200;
index = pan_tompkin(signal,Fs);
index = index(2:end-1);
idx_dur = (index(end) - index(1))/Fs;
idx_cnt = length(index)-1;
idx_int = idx_dur/idx_cnt;
HR = round(60/idx_int);
end
This is my Matlab code
MLApp.MLApp matlab = new MLApp.MLApp();
matlab.Execute(#"cd C:\Users\User\Desktop");
object result = null;
matlab.Feval("getHR", 1, out result,input_Data);
object[] res = result as object[];
tmp_HR = Convert.ToInt32(res[0]);
And this is part of my .net code where calling Matlab function
input_Data is 2000x1 double array
When I run this program, error is occur that "Undefined function 'getHR' for input arguments of type 'double'." on Matlab.Feval line
Someone advised me to 'varargin' to solve this problem, but I can not find the answer(I don't think it is necessary)
How can I revise my code to fix this problem?
I can not use the way to use Matlab compiler SDK or Matlab coder
maybe have to use only MLApp to solve this problem
So I want to build this little code sandbox in Unity, which would allow me to teach students the basics of algorithmics and coding.
The idea would be for them to enter (very basic) code in a text box or something of the kind, and to observe the effects of their code onto objects present in a Unity scene. I'm pretty sure this has been done a million times, but I'd love to try my hand at this. The rub is, I have no idea where to start...
I guess the idea is that the string would be compiled into code & executed at runtime, at the press of a button.
I've read about numerous other questions on SO, and have come up with very diverse solutions such as using a C# parser, reflection, expression trees, CodeDom, etc.
From what I understood of all these (i.e., not much), CodeDom seemed more appropriate, but then I read that it only ran inside of Visual Studio and generated errors in public builds. So does that mean that this is going to be a problem within Unity3D (as it is based on Mono?)
Thank you for your help,
In the following case, you look for an existing method of the given name on the same script (you can easily convert it to another script or any script in the assembly (not recommended though)):
string actionStr = inputField.text;
Type t = this.GetType();
MethodInfo mi = t.GetMethod(actionStr);
if(mi == null)
{
ErrorMethod(actionStr + " method could not be found");
}else
{
mi.Invoke(this);
}
Another way would be to store all the methods in a dictionary (faster):
Dictionary<string, Action>dict = null;
void Start()
{
this.dict = new Dictionary<string, Action>();
this.dict.Add("dosomething", DoSomething);
}
void DoSomething(){}
public void OnActionCall(string inputFieldStr)
{
string str = inputFieldStr.ToLower();
if(this.dict.Contains(str) == false)
{
ErrorMethod(actionStr + " method could not be found");
return;
}
this.dict[str]();
}
Since CompileAssemblyFromSource add custom functions in a smart way was ignored im going to ask this question differently so people will bother to read it.
cutting at the chase,i am making a language by "translating" the new syntax into c# and compiling it in memory in this fashion.
using (Microsoft.CSharp.CSharpCodeProvider CodeProv =
new Microsoft.CSharp.CSharpCodeProvider())
{
CompilerResults results = CodeProv.CompileAssemblyFromSource(
new System.CodeDom.Compiler.CompilerParameters()
{
GenerateInMemory = true
},
code);
var type = results.CompiledAssembly.GetType("MainClass");
var obj = Activator.CreateInstance(type);
var output = type.GetMethod("Execute").Invoke(obj, new object[] { });
Console.WriteLine(output);
}
basically i am executing a "main" function written inside the code variable.
and i am using some functions in the code variable i would like to include without adding it as a string at the bottom like this:
code += #"public void Write(string path, object thevar)
{
if (thevar.GetType() == typeof(string))
{
System.IO.File.WriteAllText(path,(string)thevar);
}
if (thevar.GetType() == typeof(string[]))
{
System.IO.File.WriteAllLines(path,(string[])thevar);
}
}";
Can i somehow add a class from my Actual main project in VS and let the compiled in memory code access it? without adding it as a string.
You can embed your source code file(s) as resources. With this technique you can edit the file in Visual Studio and access the contents of the files as if it was a string during run-time.
This link shows how to do it:
https://stackoverflow.com/a/433182/540832
I have some code written in Matlab however I wish to call this code from a C# console application.
I do not require any data to be returned from Matlab to my app (although if easy would be nice to see).
There appears to be a few options however not sure which is best. Speed is not important as this will be an automated task.
MATLAB has a .Net interface that's well-documented. What you need to do is covered in the Call MATLAB Function from C# Client article.
For a simple MATLAB function, say:
function [x,y] = myfunc(a,b,c)
x = a + b;
y = sprintf('Hello %s',c);
..it boils down to creating an MLApp and invoking the Feval method:
class Program
{
static void Main(string[] args)
{
// Create the MATLAB instance
MLApp.MLApp matlab = new MLApp.MLApp();
// Change to the directory where the function is located
matlab.Execute(#"cd c:\temp\example");
// Define the output
object result = null;
// Call the MATLAB function myfunc
matlab.Feval("myfunc", 2, out result, 3.14, 42.0, "world");
// Display result
object[] res = result as object[];
Console.WriteLine(res[0]);
Console.WriteLine(res[1]);
Console.ReadLine();
}
}
In my WinForms application I need to call javascript function from my WebBrowser control. I used Document.InvokeScript and it works perfect with functions alone e.g
Document.InvokeScript("function").
But when i want to call javascript object method e.g.
Document.InvokeScript("obj.method")
it doesn't work. Is there a way to make it work? Or different solution to this problem? Without changing anything in the javascript code!
Thanks in advance :)
The example in the documentation does NOT include the parenthesis.
private void InvokeScript()
{
if (webBrowser1.Document != null)
{
HtmlDocument doc = webBrowser1.Document;
String str = doc.InvokeScript("test").ToString() ;
Object jscriptObj = doc.InvokeScript("testJScriptObject");
Object domOb = doc.InvokeScript("testElement");
}
}
Try
Document.InvokeMethod("obj.method");
Note that you can pass arguments if you use HtmlDocument.InvokeScript Method (String, Object[]).
Edit
Looks like you aren't the only one with this issue: HtmlDocument.InvokeScript - Calling a method of an object . You can make a "Proxy function" like the poster of that link suggests. Basically you have a function that invokes your object's function. It's not an ideal solution, but it'll definitely work. I'll continue looking to see if this is possible.
Another post on same issue: Using WebBrowser.Document.InvokeScript() to mess around with foreign JavaScript . Interesting solution proposed by C. Groß on CodeProject:
private string sendJS(string JScript) {
object[] args = {JScript};
return webBrowser1.Document.InvokeScript("eval",args).ToString();
}
You could make that an extension method on HtmlDocument and call that to run your function, only using this new function you WOULD include parenthesis, arguments, the whole nine yards in the string you pass in (since it is just passed along to an eval).
Looks like HtmlDocument does not have support for calling methods on existing objects. Only global functions. :(
Unfortunately you can't call object methods out of the box using WebBrowser.Document.InvokeScript.
The solution is to provide a global function on the JavaScript side which can redirect your call. In the most simplistic form this would look like:
function invoke(method, args) {
// The root context is assumed to be the window object. The last part of the method parameter is the actual function name.
var context = window;
var namespace = method.split('.');
var func = namespace.pop();
// Resolve the context
for (var i = 0; i < namespace.length; i++) {
context = context[namespace[i]];
}
// Invoke the target function.
result = context[func].apply(context, args);
}
In your .NET code you would use this as follows:
var parameters = new object[] { "obj.method", yourArgument };
var resultJson = WebBrowser.Document.InvokeScript("invoke", parameters);
As you mention that you cannot change anything to your existing JavaScript code, you'll have to inject the above JavaScript method in some how. Fortunately the WebBrowser control can also do for you by calling the eval() method:
WebBrowser.Document.InvokeScript("eval", javaScriptString);
For a more robust and complete implementation see the WebBrowser tools I wrote and the article explaining the ScriptingBridge which specifically aims to solve the problem you describe.
webBrowser.Document.InvokeScript("execScript", new object[] { "this.alert(123)", "JavaScript" })
for you supposed to be like this
webBrowser.Document.InvokeScript("execScript", new object[] { "obj.method()", "JavaScript" })