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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:16:36+00:00 2026-05-29T05:16:36+00:00

I have a non-static C# class with some instance methods, which I need to

  • 0

I have a non-static C# class with some instance methods, which I need to call from IronPython scripts. Currently I’m doing it this way:

scope.SetVariable("class", instanceOfClass);

in C# code and

class.SomeMethod(args)

in script.

What I want is being able to call this class methods without adding class. each time in the script. Each script has its own instance of the class, and only one instance is used in one script.

If this class was static, the solution would be from ClassName import *, but as I know there is no similar construction for non-static classes.

How can this be done? I have some ideas (such as using reflection, or adding class. to each call in Python source programmatically), but they are overcomplicated and may be even not possible to implement.

UPD:

Problem solved by using such python code (before actual script):

def Method1(arg1): # for simple method
    class.Method1(arg1)

def Method2(arg = 123): # for default values
    class.Method2(arg)

def Method3(*args): # for params
    class.Method3(args)

# so on
  • 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-29T05:16:36+00:00Added an answer on May 29, 2026 at 5:16 am

    from ClassName import * is actually from namespace import type. This statement makes the type avaiable for use via the type name in Python. It makes no difference if the class is static or not. Consider this sample code – Environment being the static class.

    import clr
    from System import Environment
    print Environment.CurrentDirectory
    

    To solve your problem, inject a delegate to the class function into your ScriptScope, rather than the class itself.

    Sample class

     public class Foo {
            public string GetMyString(string input) {
                return input;
            }
        }
    

    Usage

     private static void Main(string[] args) {
                ScriptEngine engine = Python.CreateEngine();
    
                string script = "x = GetMyString('value')";
    
                Foo foo = new Foo();
    
                ScriptSource scriptSource = engine.CreateScriptSourceFromString(script);
    
                ScriptScope scope = engine.CreateScope();
                scope.SetVariable("GetMyString", new Func<string, string>(foo.GetMyString));
    
                scriptSource.Execute(scope);
    
                string output = scope.GetVariable<string>("x");
                Console.WriteLine(output);
            }
    

    prints

    value

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

Sidebar

Related Questions

I have a use case where I need to call a (non-static) method in
I have a non-static class in which i have several properties, ie serverURL, serverPort
I have a class that must have some static methods. Inside these static methods
I have a static field in a non static class. public class DBtools {
So in java, say you have a non-static method 'bar()' in an class 'Foo'.
I have a class with both a static and a non-static interface in C#.
I have a non-visual component which manages other visual controls. I need to have
Possible Duplicate: non static method cannot be referenced from static context hey i have
I have some information stored as SharedPreferences. I need to access that information from
I have a public static property in a class. The class has some custom

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.