views:

151

answers:

3

Can anyone tell me if its possible to redeclare a C# class in IronPython? If I have a C# class, would I be able to monkey-patch it from IronPython?

A: 

Why would you redeclare it? Wouldn't it make more sense to subclass it or just make a totally new class?

EDIT: Give your clarification, I think what you actually need is a proper inversion of control (IOC) framework, such as Castle Windsor. Basically, you have a database interface, then configure the IOC framework to inject a real database for production, and mock database for testing.

Matthew Flaschen
I want to redeclare the constructor because the class tries to access the database in the constructor and that makes it difficult to unit test. By redeclaring the constructor I can point the class to a mock database and unit test it without hitting the actual database. Thanks for responding.
Wesley Wiser
A: 

You can monkey-patch from IronPython, but IPy is the only environment that will respect your changes; i.e. if you tried to mock out File.Create from IronPython, this would work fine for any IPy code, but if you called a C# method which called File.Create, it would get the real one, not the mock.

Paul Betts
How do you do this? When I try it on my own classes I get this message:AttributeError: attribute 'methodName' of 'className' object is read-only
Wesley Wiser
A: 

You cannot monkey patch from IronPython. IronPython treats all .NET classes just like CPython treats built-in types: they cannot be monkey patched. IronRuby on the other hand does support this.

Dino Viehland