IronrubyスクリプトのメソッドパラメーターとしてC#ジェネリックを使用する

StackOverflow https://stackoverflow.com/questions/5344181

  •  27-10-2019
  •  | 
  •  

質問

私のジレマについては、以下のコードを参照してください。私は、正常に動作するIlist(Countchildren)のアイテムのカウントを返す方法を持つオブジェクトを持っています。しかし、同じことをするが、一般的な(countGenericChildren)を取り入れる別のものはそうではありません。スクリプトを実行している行に「System.NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていない」を取得します(コメントを参照)。最後の2つの主張は実行されません。

これは、ジェネリックをパラメーターとして渡すことと関係があると思いますが、Ironrubyの私の知識は非常に限られています。どんな助けも感謝します。 C#V3.5、IronRuby V1.0

    [Test]
    public void TestIronRubyGenerics()
    {
        string script = null;
        object val;
        ScriptRuntime _runtime;
        ScriptEngine _engine;
        ScriptScope _scope;

        _runtime = Ruby.CreateRuntime();
        _engine = _runtime.GetEngine("ruby");
        _scope = _runtime.CreateScope();

        _scope.SetVariable("parentobject", new ParentObject());


        // non-generic
        script = "parentobject.CountChildren(parentobject.Children)";
        val = _engine.CreateScriptSourceFromString(script, SourceCodeKind.Expression).Execute(_scope);
        Assert.IsTrue(val is int);
        Assert.AreEqual(2, val);

        // generic - this returns correctly
        script = "parentobject.GenericChildren";
        val = _engine.CreateScriptSourceFromString(script, SourceCodeKind.Expression).Execute(_scope);
        Assert.IsTrue(val is IList<ChildObject>);

        // generic - this does not
        script = "parentobject.CountGenericChildren(parentobject.GenericChildren)";
        val = _engine.CreateScriptSourceFromString(script, SourceCodeKind.Expression).Execute(_scope);
        Assert.IsTrue(val is bool);
        Assert.AreEqual(2, val);
        return;
    }

    internal class ParentObject
    {
        private IList<ChildObject> list;

        public ParentObject()
        {
            list = new List<ChildObject>();
            list.Add(new ChildObject());
            list.Add(new ChildObject());
        }

        public IList<ChildObject> GenericChildren
        {
            get
            {
                return list;
            }
        }

        public IList Children
        {
            get
            {
                IList myList = new System.Collections.ArrayList(list.Count);
                foreach(ChildObject o in list)
                    myList.Add(o);
                return myList;
            }
        }

        public int CountGenericChildren(IList<ChildObject> c)
        {
            return c.Count;
        }

        public int CountChildren(IList c)
        {
            return c.Count;
        }
    }

    internal class ChildObject
    {
        public ChildObject()
        {
        }
    }
役に立ちましたか?

解決

それはIronrubyのバグです。回避するには、変更します CountGenericChildren 受信する方法 List それ以外の IList:

public int CountGenericChildren(List<ChildObject> c)
{
  return c.Count;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top