[Ironruby-core] Accessing a Module's Classes from C# with dynamic

Tomas Matousek Tomas.Matousek at microsoft.com
Fri Feb 25 03:25:11 EST 2011


Wouldn't this work?

dynamic globalConstants = engine.Runtime.Globals

globalConstants.MyModule.const_get('MyClass')

assuming

module MyModule
  class MyClass
  end
end

Haven't tried. Let me know if it works.

Tomas

From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Shay Friedman
Sent: Thursday, February 24, 2011 10:38 PM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Accessing a Module's Classes from C# with dynamic

I got it working using a kind of a hack/workaround.

I know this is kind of a hack/workaround, but I managed to do it this way:

I added the next code to the end of the ruby file:

    def hack(s)
      eval(s)
    end

And then used it from the C# code to get the class object:

    var engine = Ruby.CreateEngine();

    var scope = engine.ExecuteFile(@"c:\code\generator\lib\generator\generator_cmd_line.rb");

    var genCmdLineObj = engine.Execute(String.Format("hack('{0}::{1}')", "Generator", "CmdLine"), scope);

    var cmdLineObj = engine.Operations.CreateInstance(genCmdLineObj);
    var results = engine.Operations.InvokeMember(cmdLineObj, "run");
    return Content(results);

Kind of a hack, but hey, it works! :)

Shay.
--------------------------------------------------------
Shay Friedman |  Co-Founder @ CodeValue<http://codevalue.net/> | C#/IronRuby MVP | Author of IronRuby Unleashed
Blog: http://IronShay.com<http://ironshay.com/> | Twitter: http://twitter.com/ironshay



On Thu, Feb 24, 2011 at 4:41 PM, Rob R. <lists at ruby-forum.com<mailto:lists at ruby-forum.com>> wrote:
to add directories to your search path from within C# you can do
something like this:


var engine  = Ruby.CreateEngine();
var searchPaths = engine.GetSearchPaths().ToList();
searchPaths.Add(@"c:\code\generator\lib");
searchPaths.Add(@"C:\Ruby-ri-192\lib\ruby\1.9.1");
engine.SetSearchPaths(searchPaths);

Let me know if you find out how to access classes that are within
modules from C#.  I'm having no luck.

--
Posted via http://www.ruby-forum.com/.
_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org<mailto:Ironruby-core at rubyforge.org>
http://rubyforge.org/mailman/listinfo/ironruby-core

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20110225/6ee60552/attachment.html>


More information about the Ironruby-core mailing list