COMからの戻り値に、配列を値に持つScripting.Dictionaryを使います。
しかし、
>||
namespace MyTest
{
class Class1
{
static Class1()
{
Scripting.Dictionary dic = new Scripting.Dictionary();
Object[] ary = new Object[2];
Object str1 = "A";
Object str2 = "B";
ary[0] = str1;
ary[1] = str2;
Object key = "key";
dic.Add(ref key, ref ary);
}
}
}
||<
の様に実装すると、
>||
引数 '2': 'ref object[]' から 'ref object' に変換できません。
||<
とエラーが出てビルドできません。
なお、VBScriptに配列はObjectの配列として返さなければなりません。
どう実装を修正すればよいでしょうか?
いちおう、これでいけました。
using System; using System.Collections.Generic; using System.Text; namespace ClassLibrary1 { public class Class1 { static Scripting.Dictionary dic = new Scripting.Dictionary(); static Class1() { Object[] w_ary = new Object[2]; Object str1 = "A"; Object str2 = "B"; w_ary[0] = str1; w_ary[1] = str2; Object key = "key"; Object ary = w_ary; // Object型に代入する dic.Add(ref key, ref ary); } public object test1() { return dic; } } }
----
おまけ。VB.NETでのテストコード
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim cs1 As New ClassLibrary1.Class1 Dim dic As Object = cs1.test1() Dim dat As Object = dic("key") Debug.Print(dat(0)) Debug.Print(dat(1)) End Sub End Class
残念ながら、質問文では「はてな記法」は使用できないようです。
要望は「はてなアイデア(http://i.hatena.ne.jp/)」にて受け付けているようですので
提案してみるのも良いかもしれません。
過去の要望で回答で記法が使えるようになりましたが、質問文ではまだ実装されていないのが現状のようです。
■ 質問や回答で「はてな記法」が利用できるように。
http://i.hatena.ne.jp/idea/742
対応してもらえると大変便利なのですが・・。
ありがとうございました。