Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8846231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:55:50+00:00 2026-06-14T11:55:50+00:00

I’m building a C# WebKit Web browser that can be automated with IronPython to

  • 0

I’m building a C# WebKit Web browser that can be automated with IronPython to aid with Quality Assurance testing. I’ll be creating test plans with IronPython that will run a number of the browser methods, providing the arguments, and evaluating the results.

Most of the documentation for IronPython illustrates how to call IronPython methods with C#, yet I’ve figured out how to set arguments to a method, and how to retrieve a methods return values, but not from the same method. You’ll note in the example below, I’m passing arguments to a method, which in turn is setting a class member variable, and then retrieving that value with another method.

Can anyone suggest a more elegant pattern?

using System;
using System.Windows.Forms;
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

namespace PythonScripting.TestApp
{

 public partial class Form1 : Form
 {
   private ScriptEngine m_engine = Python.CreateEngine();
   private ScriptScope m_scope = null;

   //used to call funcs by Python that dont need to return vals
   delegate void VoidFunc(string val);

   public Form1()
   {
      InitializeComponent();
   }

   private void doSomething()
   {
       MessageBox.Show("Something Done", "TestApp Result");
   }

   private string _rslt = "";

   private string getSomething()
   {
      return _rslt;
   }

   private void setSomething(string val)
   {
       _rslt = val;
   }

   private void Form1_Load(object sender, EventArgs e)
   {
       m_scope = m_engine.CreateScope();

       Func<string> PyGetFunction = new Func<string>(getSomething);
       VoidFunc PySetFunction = new VoidFunc(setSomething);

       m_scope.SetVariable("txt", txtBoxTarget);
       m_scope.SetVariable("get_something", PyGetFunction);
       m_scope.SetVariable("set_something", PySetFunction);           
   }

   private void button1_Click(object sender, EventArgs e)
   {
       string code = comboBox1.Text.Trim();
       ScriptSource source = m_engine.CreateScriptSourceFromString(code, SourceCodeKind.SingleStatement);

       try
       {
          source.Execute(m_scope);
          Func<string> result = m_scope.GetVariable<Func<string>>("get_something");

          MessageBox.Show("Result: " + result(), "TestApp Result");
       }
       catch (Exception ue)
       {
           MessageBox.Show("Unrecognized Python Error\n\n" + ue.GetBaseException(), "Python Script Error");
       }
   }  
 }
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:55:51+00:00Added an answer on June 14, 2026 at 11:55 am

    Under normal circumstances, IronPython only has access to public classes and members on .Net types. You should be able to just set your object as a variable in the scope and access any of the public members on that object from your scripts. However the methods you have in your example are private so therefore you can’t really access them. If you wish to be able to access them, you need to expose the non-public members somehow so you can manipulate them or use reflection.

    A pattern you could try would be to add a method that would create a proxy for your object that would have all the methods you would want to expose. Then add that proxy object to your scope then use that proxy to call your methods.

    public partial class MyForm : Form
    {
        private readonly ScriptEngine m_engine;
        private readonly ScriptScope m_scope;
    
        public MyForm()
        {
            InitializeComponent();
            m_engine = Python.CreateEngine();
    
            dynamic scope = m_scope = m_engine.CreateScope();
            // add this form to the scope
            scope.form = this;
            // add the proxy to the scope
            scope.proxy = CreateProxy();
        }
    
        // public so accessible from a IronPython script
        public void ShowMessage(string message)
        {
            MessageBox.Show(message);
        }
    
        // private so not accessible from a IronPython script
        private int MyPrivateFunction()
        {
            MessageBox.Show("Called MyForm.MyPrivateFunction");
            return 42;
        }
    
        private object CreateProxy()
        {
            // let's expose all methods we want to access from a script
            dynamic proxy = new ExpandoObject();
            proxy.ShowMessage = new Action<string>(ShowMessage);
            proxy.MyPrivateFunction = new Func<int>(MyPrivateFunction);
            return proxy;
        }
    }
    

    With this, you can execute your scripts accessing your form through the form variable or the proxy through the proxy variable. For good measure, here’s how you could easily access variables and other objects from the scope.

    private void DoTests()
    {
        // try to call the methods on the form or proxy objects
        var script = @"
    form.ShowMessage('called form.ShowMessage')
    # formFuncResult = form.MyPrivateFunction() # fail, MyPrivateFunction is not accessible
    proxy.ShowMessage('called proxy.ShowMessage')
    proxyFuncResult = proxy.MyPrivateFunction() # success, MyPrivateFunction on the proxy is accessible
    ";
        m_engine.Execute(script, m_scope);
    
        // access the scope through a dynamic variable
        dynamic scope = m_scope;
    
        // get the proxyFuncResult variable
        int proxyFuncResult = scope.proxyFuncResult;
        MessageBox.Show("proxyFuncResult variable: " + proxyFuncResult);
    
        // call the the function on the proxy directly
        int directResult = scope.proxy.MyPrivateFunction();
        MessageBox.Show("result of MyPrivateFunction: " + directResult);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.