获取脚本文件中的所有方法

发布于 2024-09-06 03:03:53 字数 249 浏览 4 评论 0原文

执行此代码后:

    var runtime = IronRuby.Ruby.CreateRuntime();
    var engine = IronRuby.Ruby.CreateEngine();
    var scrope = engine.CreateScope();
    engine.ExecuteFile("libtest.rb");

如何在 C# 代码中获取 ruby​​ 类的所有方法?

After executing this code:

    var runtime = IronRuby.Ruby.CreateRuntime();
    var engine = IronRuby.Ruby.CreateEngine();
    var scrope = engine.CreateScope();
    engine.ExecuteFile("libtest.rb");

How can I get all the methods of a ruby class in c# code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠魁 2024-09-13 03:03:58

或者您可以从 C# http://github .com/casualjim/ironrubymvc/blob/master/IronRubyMvc/Core/RubyEngine.cs#L178

如果您定义方法但没有将它们放在类中,则应将它们添加到 Object 类中

Or you can do it from C# http://github.com/casualjim/ironrubymvc/blob/master/IronRubyMvc/Core/RubyEngine.cs#L178

If you define methods without having them in a class they should get added to the Object class

玩物 2024-09-13 03:03:56

我还没有弄清楚所有事情,但这是一个开始:

using System;
using IronRuby;
using Microsoft.Scripting.Hosting;

class IronRubyReflection
{
    static void Main(string[] args)
    {
        var engine = Ruby.CreateEngine();
        var scope = engine.ExecuteFile("libtest.rb");
        dynamic globals = engine.Runtime.Globals;

        var klass = globals.Klass;
        var klass_s = klass.GetOrCreateSingletonClass();
        var modul = globals.Modul;
        var modul_s = modul.GetOrCreateSingletonClass();

        Console.WriteLine(
            scope.GetVariable<IronRuby.Builtins.RubyMethod>(
                "method_in_the_global_object").Name);

        Action<string, IronRuby.Builtins.RubyModule,
            IronRuby.Runtime.Calls.RubyMemberInfo> classprinter =
                (n, k, i) => { Console.WriteLine(n, k, i); };

        klass.ForEachMember(false,
            IronRuby.Runtime.RubyMethodAttributes.Default, classprinter);
        klass_s.ForEachMember(false,
            IronRuby.Runtime.RubyMethodAttributes.Default, classprinter);
        modul.ForEachMember(false,
            IronRuby.Runtime.RubyMethodAttributes.Default, classprinter);
        modul_s.ForEachMember(false,
            IronRuby.Runtime.RubyMethodAttributes.Default, classprinter);

        Console.ReadLine();
    }
}

请原谅我的风格,我实际上并不了解 C#。

这是我的 libtest.rb:

def method_in_the_global_object; end

class Klass
  def instance_method_in_class; end
  def self.class_method; end
end

class Modul
  def instance_method_in_module; end
  def self.module_method; end
end

local = Object.new
def local.singleton_meth; end

@instance = Object.new
def @instance.singleton_meth; end

$global = Object.new
def $global.singleton_meth; end

这是输出:

method_in_the_global_object
instance_method_in_class
class_method
Equals
ReferenceEquals
allocate
clr_constructor
clr_ctor
clr_new
new
superclass
instance_method_in_module
module_method
Equals
ReferenceEquals
allocate
clr_constructor
clr_ctor
clr_new
new
superclass

I haven't figured everything out yet, but here's a start:

using System;
using IronRuby;
using Microsoft.Scripting.Hosting;

class IronRubyReflection
{
    static void Main(string[] args)
    {
        var engine = Ruby.CreateEngine();
        var scope = engine.ExecuteFile("libtest.rb");
        dynamic globals = engine.Runtime.Globals;

        var klass = globals.Klass;
        var klass_s = klass.GetOrCreateSingletonClass();
        var modul = globals.Modul;
        var modul_s = modul.GetOrCreateSingletonClass();

        Console.WriteLine(
            scope.GetVariable<IronRuby.Builtins.RubyMethod>(
                "method_in_the_global_object").Name);

        Action<string, IronRuby.Builtins.RubyModule,
            IronRuby.Runtime.Calls.RubyMemberInfo> classprinter =
                (n, k, i) => { Console.WriteLine(n, k, i); };

        klass.ForEachMember(false,
            IronRuby.Runtime.RubyMethodAttributes.Default, classprinter);
        klass_s.ForEachMember(false,
            IronRuby.Runtime.RubyMethodAttributes.Default, classprinter);
        modul.ForEachMember(false,
            IronRuby.Runtime.RubyMethodAttributes.Default, classprinter);
        modul_s.ForEachMember(false,
            IronRuby.Runtime.RubyMethodAttributes.Default, classprinter);

        Console.ReadLine();
    }
}

Forgive my style, I don't actually know C#.

This is my libtest.rb:

def method_in_the_global_object; end

class Klass
  def instance_method_in_class; end
  def self.class_method; end
end

class Modul
  def instance_method_in_module; end
  def self.module_method; end
end

local = Object.new
def local.singleton_meth; end

@instance = Object.new
def @instance.singleton_meth; end

$global = Object.new
def $global.singleton_meth; end

And this is the output:

method_in_the_global_object
instance_method_in_class
class_method
Equals
ReferenceEquals
allocate
clr_constructor
clr_ctor
clr_new
new
superclass
instance_method_in_module
module_method
Equals
ReferenceEquals
allocate
clr_constructor
clr_ctor
clr_new
new
superclass

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文