I wrote the following Ruby code file:
require 'MyAssembly.dll'
class MyClass
def initialize(obj)
System::Diagnostics::WriteLine('test')
@var = MyAssembly::Namespace::CSharpClass.new
end
end
MyClass
CSharpClass is a class with one default constructor. Now, I work with this file like that:
dynamic MyClass = RubyEngine.Execute(script)
//script is a string which contains the aforementioned Ruby code as text
MyClass.@new(some_obj)
Note that MyClass has a constructor with one parameter. When I run the program, I get the following exception: ArgumentException - wrong number of arguments (0 for 1) at the last C# code line.
If I change CSharpClass to any other name there is an exception saying that I there is no such objects (so I'm guessing that assembly loading worked ok).
Now the fun part: if I change the second line in the initialize(obj) method to
@var = MyAssembly::Namespace::CSharpClass.new(nil)
then there is a long list of "test" strings in the Debug output and a StackOverflowException. So it looks like recursive MyClass object instantiating, i.e. CSharpClass.new(nil) somehow executes as MyClass.new(nil). Probably that's why I got the AgrumentException above.
I have tried both IronRuby 1.0 and 1.1.3 but to no avail. What have I missed?
UPDATE
More fun! Now assume the Ruby code looks like this:
require 'MyAssembly.dll'
class MyClass
def initialize(a, b)
@var = MyAssembly::Namespace::CSharpClass.new
end
end
MyClass.new(0, 0)
If I try to call CSharpClass.new with different arguments I get mutually exclusive exception messages:
CSharpClass.new # wrong number of arguments (0 for 1)
CSharpClass.new(0) # wrong number of arguments (1 for 2)
CSharpClass.new(0, 0) # wrong number of arguments (2 for 1)
...
CSharpClass.new(<N args>) # wrong number of arguments (N for 1)
I'll be very glad if somebody knows what I am doing wrong - I'd really like to use IronRuby in my project.
Well, that's nice and awful at the same time. It turned out that I began my file with the following lines:
def initialize(obj)
MyClass.new(obj)
end
I.e. I have used the MyClass before I have declared it. Removing those lines solved the problem. It's a wonder what kind of exceptions can dynamic code produce when something goes wrong...