[Ironruby-core] Throwing Exceptions defined in Ruby from C#
Tomas Matousek
Tomas.Matousek at microsoft.com
Thu Feb 12 22:33:09 EST 2009
Why don't you declare the exceptions in C#? I assume the library is primarily to be used from Ruby code, not from C#, right?
Tomas
-----Original Message-----
From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Daniele Alessandri
Sent: Thursday, February 12, 2009 6:31 AM
To: ironruby-core at rubyforge.org
Subject: [Ironruby-core] Throwing Exceptions defined in Ruby from C#
Hi,
I'm refactoring and polishing the source code of my port of json/ext
to IronRuby (it's feature complete by now) but I'm wondering if there
is a better way to throw an Exception defined in ruby from C# compared
to the only solution I've came up with so far. Here is the code I'm
using right now, stripped down of checks and condensed in one method
just for the sake of brevity:
----- ruby -----
module JSON
class JSONError < StandardError; end
class ParserError < JSONError; end
end
----- C# -----
public static void RaiseParserError(RubyScope scope, String msg) {
RubyModule eParserError;
scope.RubyContext.TryGetModule(scope.GlobalScope,
"JSON::ParserError",
out eParserError
);
RubyClass exceptionClass = eParserError as RubyClass;
Type underlyingType = exceptionClass.GetUnderlyingSystemType();
BindingFlags bindingFlags = BindingFlags.NonPublic |
BindingFlags.Public |
BindingFlags.Instance;
ConstructorInfo constructor = underlyingType.GetConstructor(
bindingFlags,
null,
new[] { typeof(RubyClass), typeof(String) },
null
);
Exception exceptionInstance = constructor.Invoke(
new object[] { exceptionClass, msg }
) as Exception;
throw exceptionInstance;
}
--
Daniele Alessandri
http://www.clorophilla.net/blog/
_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core
More information about the Ironruby-core
mailing list