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

  • Home
  • SEARCH
  • 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 8901433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:21:13+00:00 2026-06-15T01:21:13+00:00

Is it possible to declare IronPython classes as Export and thus add them to

  • 0

Is it possible to declare IronPython classes as “Export” and thus add them to a MEF catalog that a
host C# application can Import?

I cannot really find any concrete examples of this, just speculation.

Here is how I have manually loaded a Python class that implements a .NET interface:

https://github.com/versionone/VersionOne.SDK.Experimental

I would like to be able to put attributes on the python classes similar to how you do it in C#.
(Or something equivalent)

Has anyone tried this?

Thanks,
Josh

  • 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-15T01:21:15+00:00Added an answer on June 15, 2026 at 1:21 am

    For those interested, I found a project on GitHub that had done this, but was a little bit coupled into the project. With the author’s approval I’ve created a new repo, IronPythonMef, and a NuGet package for it.

    There’s additional discussion in this thread at GitHub.

    Here’s an example of how it works:

    First, an interface declared in C#:

    namespace IronPythonMef.Tests.Example.Operations
    {
        public interface IOperation
        {
            object Execute(params object[] args);
            string Name { get; }
            string Usage { get; }
        }
    }
    

    An implementation that Exports that interface in C#:

    [Export(typeof(IOperation))]
    public class Power : IOperation
    {
        public object Execute(params object[] args)
        {
            if (args.Length < 2)
            {
                throw new ArgumentException(Usage, "args");
            }
    
            var x = Convert.ToDouble(args[0]);
            var y = Convert.ToDouble(args[1]);
    
            return Math.Pow(x, y);
        }
    
        public string Name
        {
            get { return "pow"; }
        }
    
        public string Usage
        {
            get { return "pow n, y -- calculates n to the y power"; }
        }
    }
    

    And, an implementation of IOperation in IronPython:

    @export(IOperation)
    class Fibonacci(IOperation):
        def Execute(self, n):
            n = int(n)
            if n == 0:
                return 0
            elif n == 1:
                return 1
            else:
                return self.Execute(n-1) + self.Execute(n-2)
    
        @property
        def Name(self):
            return "fib"
    
        @property
        def Usage(self):
            return "fib n -- calculates the nth Fibonacci number"
    

    And here’s a test case of a class that imports these operations from both C# and IronPython and executes them:

    [TestFixture]
    public class MathWizardTests
    {
        [Test]
        public void runs_script_with_operations_from_both_csharp_and_python()
        {
            var mathWiz = new MathWizard();
    
            new CompositionHelper().ComposeWithTypesExportedFromPythonAndCSharp(
                mathWiz,
                "Operations.Python.py",
                typeof(IOperation));
    
            const string mathScript =
    @"fib 6
    fac 6
    abs -99
    pow 2 4
    ";
            var results = mathWiz.ExecuteScript(mathScript).ToList();
    
            Assert.AreEqual(8, results[0]);
            Assert.AreEqual(720, results[1]);
            Assert.AreEqual(99f, results[2]);
            Assert.AreEqual(16m, results[3]);
        }
    }      
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to declare a function pointer (non C++ 11) that can point
Is it possible to declare a composite-component in such a way that it can
Possible Duplicate: declare property as object? in java you can create an object directly
Possible Duplicate: declare property as object? class core { public $dbh = new PDO(mysql:dbname=newdbnaem;host=1.1.1.1:1111,
In c++, is it possible to declare inner class (CInner) such that it has
Is it possible to declare variables in Excel cells and use them as parameters
Is it possible to declare a constant Guid in C#? I understand that I
is it possible to declare an entity property so that it is reset to
Is it possible to declare a style that sets the fontsize of tooltips to
Is it possible to declare a class in C# that is only valid to

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.