在 IronRuby 中使用重载构造函数实例化对象时出现问题?

发布于 2024-11-15 01:21:07 字数 938 浏览 3 评论 0原文

我有 c# 类“Document”,带有重载构造函数 (int id)、(guid id) 和一些更多参数选项 - 并且没有 0 个参数的重载。当我尝试使用 IronRuby 创建新对象时,我绊倒了。它说它想要有 0 个参数,所以:

d = Document.new

工作正常。但是当我尝试时

d = Document.new some_integer

,我收到错误消息

"wrong number of arguments (1 for 0)"

The class def 看起来像这样:

public Document(int id) : base(id)
{
  // some code
}

编辑:这是完整的代码。它位于 Umbraco 上下文中,我将命名空间转换为小写,以便能够在 IronRuby 中使用它们:

$LOAD_PATH << "C:\\inetpub-dev\\dev.mysite.com\\bin"
require "cms.dll"
require "businesslogic.dll"

Web = Object.const_get("umbraco").const_get("cms").const_get("businesslogic").const_get("web")
existing_document_id = 1065
existing_document = Web::Document.new(existing_document_id)

C# 中的相同代码:

var existingDocument = new umbraco.cms.businesslogic.web.Document(1065);

I have a c# class "Document" with overloaded constructors (int id), (guid id) and some more parameter options - and no overload with 0 parameters. When I try to create a new object with IronRuby I stumble. It says it wants to have 0 arguments, so:

d = Document.new

works fine. But when I try

d = Document.new some_integer

I get the error message

"wrong number of arguments (1 for 0)"

The class def looks like this:

public Document(int id) : base(id)
{
  // some code
}

Edit: here's the complete code. It's in an Umbraco context, and I translate namespaces with lower caseing to be able to use them in IronRuby:

$LOAD_PATH << "C:\\inetpub-dev\\dev.mysite.com\\bin"
require "cms.dll"
require "businesslogic.dll"

Web = Object.const_get("umbraco").const_get("cms").const_get("businesslogic").const_get("web")
existing_document_id = 1065
existing_document = Web::Document.new(existing_document_id)

Same code in C#:

var existingDocument = new umbraco.cms.businesslogic.web.Document(1065);

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

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

发布评论

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

评论(1

晨曦慕雪 2024-11-22 01:21:07

我想知道您是否在某个地方遇到了命名空间冲突。
Document 是否有可能在您所包含的内容中的其他地方定义?

我们看不到您的基类是什么样子
另外,我们看不到您正在使用的要求/包含内容

我设置了一个示例,它似乎按预期工作:

namespace IRConstructorParamSpike
{
    public abstract class BaseDocument
    {
        public BaseDocument(int id) { }
    }

    public class Document : BaseDocument
    {
        public Document(int id) : base(id) { }
    }

    public class SomeDocument : BaseDocument
    {
        public SomeDocument(int id) : base(id) { }
    }
}

以下是会话的样子(使用 IronRuby 1.1.4.0):

<块引用>
<块引用>

需要“IRConstructorParamSpike.dll”
=>正确

d = 文档.new
(ir):1:in `const_missing': 未初始化的常量 Object::Document (NameError)
来自 (ir):1 <-- 因为我没有包含名称空间

d = IRConstructorParamSpike::Document.new
(ir):1: 参数数量错误(0 代表 1)(ArgumentError) <-- 我们对没有无参数构造函数的期望

d = IRConstructorParamSpike::Document.new 10
=> IRConstructorParamSpike.Document


您能否尝试使用这个更简单的示例代码,看看是否可以按照您期望的方式创建对象?

I wonder if you are hitting a namespace collision somewhere.
Is there any possibility that Document is defined somewhere else in what you are including?

We can't see what your base class looks like
Also, we can't see what requires/includes you are using

I set up an example, and it seems to work as expected:

namespace IRConstructorParamSpike
{
    public abstract class BaseDocument
    {
        public BaseDocument(int id) { }
    }

    public class Document : BaseDocument
    {
        public Document(int id) : base(id) { }
    }

    public class SomeDocument : BaseDocument
    {
        public SomeDocument(int id) : base(id) { }
    }
}

Here is what the session looked like (using IronRuby 1.1.4.0):

require 'IRConstructorParamSpike.dll'
=> true

d = Document.new
(ir):1:in `const_missing': uninitialized constant Object::Document (NameError)
from (ir):1 <-- Because I didn't include the namespace

d = IRConstructorParamSpike::Document.new
(ir):1: wrong number of arguments (0 for 1) (ArgumentError) <-- What we would expect with no parameterless constructor

d = IRConstructorParamSpike::Document.new 10
=> IRConstructorParamSpike.Document

Could you try with this simpler example code and see if you can create the objects in the way you are expecting?

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