IronRuby 调用 C# 扩展方法 - 错误 - .NET 3.5 中的兼容性

发布于 2024-08-05 11:40:42 字数 658 浏览 6 评论 0 原文

我已经从 DataGridView 编写了一个名为 HideColumns 的扩展方法。

public static class Extensions
{
    public static void HideColumns(this DataGridView dataGridView, params string[] columnNames)
    {
        foreach (string str in columnNames)
        {
            if (dataGridView.Columns[str] != null)
            {
                dataGridView.Columns[str].Visible = false;
            }
        }
    }

}

我将网格作为名为 main_grid 的变量传递到 IronRuby 脚本中

当我的脚本调用时, main_grid.HideColumns("名字","姓氏") 脚本因脚本错误而崩溃 System.Windows.Forms.DataGridView:System::Windows::Forms::DataGridView 的未定义方法“HideColumns”

扩展方法似乎在 C# 中工作正常。什么给?

I have written an Extension Method off of DataGridView called HideColumns.

public static class Extensions
{
    public static void HideColumns(this DataGridView dataGridView, params string[] columnNames)
    {
        foreach (string str in columnNames)
        {
            if (dataGridView.Columns[str] != null)
            {
                dataGridView.Columns[str].Visible = false;
            }
        }
    }

}

I pass my grid into an IronRuby script as a variable called main_grid

When my script calls
main_grid.HideColumns("FirstName","LastName")
the script blows up with Error in Script
undefined method 'HideColumns' for System.Windows.Forms.DataGridView:System::Windows::Forms::DataGridView

The extension methods seem to work okay from C#. What gives?

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

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

发布评论

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

评论(3

小兔几 2024-08-12 11:40:42

FWIW,IronRuby 1.1(需要 .net 4)提供了 using_clr_extensions 方法 - 正如发行说明中所述,这会激活在给定命名空间中定义的类上定义的所有扩展方法,无论它们定义的程序集如何在;将来加载的在激活的命名空间中定义扩展方法的程序集将自动出现在正确的类型上,如下所示:

load_assembly "System.Core"
using_clr_extensions System::Linq

# ...

products.
  where(lambda { |p| p.units_in_stock == 0 }).
  each { |x| puts x.product_name }

发行说明还指出了 http://github.com/ironruby/ironruby/blob/master/Languages/Ruby/Samples/Linq/101samples .rb

FWIW, IronRuby 1.1 (needs .net 4) provides the using_clr_extensions method -- as noted in the release notes this activates all extension methods defined on classes defined in a given namespace, regardless of the assembly they are defined in; assemblies loaded in the future that define extension methods in the activated namespace will automatically appear on the correct types, like this:

load_assembly "System.Core"
using_clr_extensions System::Linq

# ...

products.
  where(lambda { |p| p.units_in_stock == 0 }).
  each { |x| puts x.product_name }

The release notes also point at a whole set of examples at http://github.com/ironruby/ironruby/blob/master/Languages/Ruby/Samples/Linq/101samples.rb

蝶…霜飞 2024-08-12 11:40:42

扩展方法只是语法糖,您需要将其调用为:

Extensions.HideColumns(main_grid, "FirstName", "LastName")

或者在 C# 中创建一个派生自 DataGridView 的新类并添加方法:

public class DataGridViewExt : DataGridView  
{
    public void HideColumns(params string[] columnNames)
    {
        foreach (string str in columnNames)
        {
            if (this.Columns[str] != null)
            {
                this.Columns[str].Visible = false;
            }
        }
    }        
}

并在表单上使用此类而不是 System.Windows.Forms 类。

The extension method is just syntatic sugar, you will need to call it as:

Extensions.HideColumns(main_grid, "FirstName", "LastName")

alternatively create a new class in C# which derives from DataGridView and add the method:

public class DataGridViewExt : DataGridView  
{
    public void HideColumns(params string[] columnNames)
    {
        foreach (string str in columnNames)
        {
            if (this.Columns[str] != null)
            {
                this.Columns[str].Visible = false;
            }
        }
    }        
}

and use this class rather than the System.Windows.Forms class on your form.

绿阴红影里的.如风往事 2024-08-12 11:40:42

既然您在 JDunkeryly 的答案的评论中提到了这一点,那么以下是您从 ruby​​ 端扩展网格的方法。只需打开类并添加一个方法(仅适用于 ruby​​ 端)。

class System::Windows::Forms::DataGridView
  def hide_columns(*columnNames)
    column_names.each do |cn|
      self.columns[cn].visible = false
    end
  end
end

至于直接使用扩展方法的建议,params 关键字对 IronRuby 来说是痛苦的。您需要使用参数构建一个类型化数组并传递它。您不能只将 ruby​​ 字符串包装在 ruby​​ 数组中。我今天早些时候在 博客中完成了这一点发布。但如果您有更顺利的方法来处理这个问题,请告诉我。

Since you mentioned it in the comments to JDunkeryly's answer, here's how you'd extend the grid from the ruby side. Just open the class and add a method (only works from the ruby side).

class System::Windows::Forms::DataGridView
  def hide_columns(*columnNames)
    column_names.each do |cn|
      self.columns[cn].visible = false
    end
  end
end

As far as the suggestion to use the extension method directly, the params keyword is painful to IronRuby. You need to build a typed array with your arguments and pass it. You can't just wrap your ruby strings in a ruby array. I've pulled this off earlier today in a blog post. But if you have a smoother way to handle that, please let me know.

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