Use IronPython to override DLL function

I would like to use IronPython to override the method from the dll, so that all future calls to this method go to the python implementation. I was hoping to base it on a technique from the accepted answer here .

So, I tried to make a dll with only the following class:

namespace ClassLibrary1
{
    public class Class1
    {
        public static string test()
        {
            return "test";
        }
    }
}

Then I did the following in IronPython:

import clr
clr.AddReference("ClassLibrary1")

import ClassLibrary1

def _override():
    return "george"

ClassLibrary1.Class1.test = _override;

print ClassLibrary1.Class1.test();

However, when I run python code, I get the following exception:

An exception of type 'System.MissingMemberException' occurred in Snippets.debug.scripting but was not handled in user code

Additional information: attribute 'test' of 'Class1' object is read-only

Is there a way to accomplish what I'm looking for?

+4
source share
2 answers

Roland, python . IronPython, , , .

# class_library_1_wrapper.py
import clr
clr.AddReference("ClassLibrary1")
import ClassLibrary1 


class Class1:
    def __init__(self):
        self._clr_object = ClassLibrary1.Class1()

    def some_method():
        return self._clr_object.SomeMethod()

    @staticmethod
    def test():
        return "george"

Cython ++. , "ClassLibrary1" IronPython Class1, :

# class_library_1_wrapper.py
import clr
clr.AddReference("ClassLibrary1")

try:
    import ClassLibrary1
except ImportError:
    print 'sorry, no speed-ups'
    # Class1 IronPython implementation here
else:
    # Implement Class1 as described in the first snippet.

Werkzeug Python.

+1

, , . , .

Monkey python, , python.

python # .

0

All Articles