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 713333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:56:57+00:00 2026-05-14T04:56:57+00:00

I have embedded an IronPython engine in a C# application. I want to expose

  • 0

I have embedded an IronPython engine in a C# application. I want to expose some custom commands (methods) to the interpreter. How do I do this?

Currently, I have something like this:

public delegate void MyMethodDel(string printText);

Main(string[] args)
{
    ScriptEngine engine = Python.CreateEngine();
    ScriptScope scope = engine.CreateScope();

    MyMethodDel del = new MyMethodDel(MyPrintMethod);
    scope.SetVariable("myprintcommand", del);

    while(true)
    {
        Console.Write(">>>");
        string line = Console.ReadLine();

        ScriptSource script = engine.CreateScriptSourceFromString(line, SourceCodeKind.SingleStatement);
        CompiledCode code = script.Compile();
        script.Execute(scope);
    }
}

void MyPrintMethod(string text)
{
    Console.WriteLine(text);
}

I can use this like this:

>>>myprintcommand("Hello World!")
Hello World!
>>>

This works fine. I wanted to know, if this is the correct way/best practice to do what I want to achieve?

How can I expose overloads of the same method. For example, if I wanted to expose a method like myprintcommand(string format, object[] args).

With the way I am currently doing it, the key “myprintcommand” can be mapped to only one delegate. Therefore I will have to change the name of the command/method if I want to expose the overloaded “myprintcommand” to the interpreter. Is there any other way to achieve what I want?

  • 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-05-14T04:56:58+00:00Added an answer on May 14, 2026 at 4:56 am

    You would probably have to write your own logic for that. Eg:

    public delegate void MyMethodDel(params object[] args);
    
    void MyPrintMethod(params object[] args)
    {
      switch (args.Length)
      {
        case 1:
          Console.WriteLine((string)args[0]);
          break;
        ...
        default:
          throw new InvalidArgumentCountException();
      }
    }
    

    This may or may not work; I am not sure how they handle the ‘params’ attribute anymore.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an embedded scripting engine in my C# application that uses IronPython 2.
I have IronPython embedded inside a C# application. I let users write IronPython scripts,
I have embedded some crystal reports in my C# application. The problem is, compiling
I have some IronPython scripts that are embedded in a C# project, and it
I have embedded a Google Map in my website. I want to change its
I have an embedded system running a busybox distribution and for some reason i
I have several embedded linux systems that I want to write a 'Who's Online?'
Can someone recommend a workaround for this ironpython bug? I have a class contained
I have embedded derby database and i work with jpa. This is my persistence.xml:
I have embedded Lua in an Objective-C application using LuaObjCBridge. I need to know

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.